From 1fce0907bbc599b8d8233c383ee1d98e05dbc0b1 Mon Sep 17 00:00:00 2001 From: bookug Date: Fri, 15 Jul 2016 16:13:04 +0800 Subject: [PATCH] add php api --- api/php/GstoreConnector.php | 64 +++++++++++++++++++++++++++++++++++++ api/php/PHPAPIExample.php | 19 +++++++++++ 2 files changed, 83 insertions(+) create mode 100644 api/php/GstoreConnector.php create mode 100644 api/php/PHPAPIExample.php diff --git a/api/php/GstoreConnector.php b/api/php/GstoreConnector.php new file mode 100644 index 0000000..9f28efa --- /dev/null +++ b/api/php/GstoreConnector.php @@ -0,0 +1,64 @@ +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); + } +} +?> + diff --git a/api/php/PHPAPIExample.php b/api/php/PHPAPIExample.php new file mode 100644 index 0000000..a8dd313 --- /dev/null +++ b/api/php/PHPAPIExample.php @@ -0,0 +1,19 @@ + . +}"; +$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; +?> +