add php api

This commit is contained in:
bookug 2016-07-15 16:13:04 +08:00
parent 9da8953769
commit 1fce0907bb
2 changed files with 83 additions and 0 deletions

View File

@ -0,0 +1,64 @@
<?php
class Connector {
private $socket;
private $port;
private $host;
public function __construct($host, $port) {
$this->host = $host;
$this->port = $port;
$this->socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if (!$this->socket) {
exit('socket creation error.');
}
$result = socket_connect($this->socket, $this->host, $this->port);
if (!$result) {
exit('socket connection error.' . $this->host);
}
}
public function send($data) {
$head = pack("L", strlen($data));
$result = socket_write($this->socket, $head . $data);
if (!$result) {
exit('message sending error');
}
return $result;
}
public function recv() {
$head = socket_recv($this->socket, $buf, 4, MSG_OOB / MSG_PEEK);
if (!$head) {
exit('message receiving error');
} else {
socket_recv($this->socket, $buf, 8192, MSG_OOB / MSG_PEEK);
return $buf;
}
}
public function build($db_name, $rdf_file_path) {
$data = "import " . $db_name . " " . $rdf_file_path . "\0";
self::send($data);
$result = self::recv();
return $result;
}
public function load($db_name) {
$data = "load " . $db_name . "\0";
self::send($data);
$result = self::recv();
return $result;
}
public function unload($db_name) {
$data = "unload " . $db_name;
self::send($data);
$result = self::recv();
return $result;
}
public function query($sparql) {
$data = "query " . $sparql . "\0";
self::send($data);
$result = self::recv();
return $result;
}
public function __desctruct() {
socket_close($this->socket);
}
}
?>

19
api/php/PHPAPIExample.php Normal file
View File

@ -0,0 +1,19 @@
<?php
require 'GstoreConnector.php';
$host = '127.0.0.1';
$port = 3305;
$dbname = "LUBM10.db";
$dbpath = "./data/LUBM_10.n3";
$query1 = "select ?x where
{
?x <ub:name> <FullProfessor0>.
}";
$build= new Connector($host,$port);
$build->build($dbname, $dbpath);
$load = new Connector($host,$port);
$load->load($dbname);
$query = new Connector($host,$port);
$result = $query->query($query1);
echo $result;
?>