add php api
This commit is contained in:
parent
9da8953769
commit
1fce0907bb
|
@ -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);
|
||||
}
|
||||
}
|
||||
?>
|
||||
|
|
@ -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;
|
||||
?>
|
||||
|
Loading…
Reference in New Issue