Class Endpoint

Description

Sparql HTTP Client for 4store Endpoint around basic php function.

You can send a query to any endpoint sparql and read the result in an array.

Example : send a simple query to DBpedia

  1.  <?php
  2.   require_once('php4store/Endpoint.php');
  3.  
  4.      $endpoint ="http://dbpedia.org/";
  5.      $sp_readonly new Endpoint($endpoint);
  6.   $q "select *  where {?x ?y ?z.} LIMIT 5";
  7.   $rows $sp_readonly->query($q'rows');
  8.   $err $sp_readonly->getErrors();
  9.   if ($err{
  10.        print_r($err);
  11.        throw new Exception(print_r($err,true));
  12.      }
  13.      var_dump($rows);
  14.  ?>

With a query ASK, you can use the parameter 'raw' in the function query and read directly the result true or false.

Example : send a query ASK with the parameter raw

  1.  <?php
  2.   require_once('php4store/Endpoint.php');
  3.  
  4.     $q "PREFIX a: <http://example.com/test/a/>
  5.             PREFIX b: <http://example.com/test/b/>
  6.             ask where { GRAPH <".$graph."> {a:A b:Name \"Test3\" .}} ";
  7.     $res $sp_readonly->query($q'raw');
  8.     $err $sp_readonly->getErrors();
  9.     if ($err{
  10.         print_r($err);
  11.         throw new Exception(print_r($err,true));
  12.     }
  13.     var_dump($res);
  14.  ?>

You can insert data also with SPARQL and the function query. If the graph doesn't exist, 4store will create the graph.

Example : send a query Insert

  1.  <?php
  2.   require_once('php4store/Endpoint.php');
  3.  
  4.  $graph "http://www.bordercloud.com";
  5.  $endpoint ="http://localhost:8080/sparql/";
  6.  
  7.      //put argument false to write
  8.      $readonly false;
  9.      $sp_write new Endpoint('http://localhost:8080/',$readonly);
  10.  
  11.      $q "
  12.              PREFIX a: <http://example.com/test/a/>
  13.              PREFIX b: <http://example.com/test/b/>
  14.              INSERT DATA {
  15.                  GRAPH <".$graph."> {
  16.                  a:A b:Name \"Test1\" .
  17.                  a:A b:Name \"Test2\" .
  18.                  a:A b:Name \"Test3\" .
  19.              }}";
  20.      $res $sp_write->query($q,'raw');
  21.      $err $sp_write->getErrors();
  22.   if ($err{
  23.          print_r($err);
  24.          throw new Exception(print_r($err,true));
  25.      }
  26.      var_dump($res);
  27.  ?>

You can delete data also with SPARQL and the function query.

Example : send a query Delete

  1.  <?php
  2.   require_once('php4store/Endpoint.php');
  3.  
  4.  $graph "http://www.bordercloud.com";
  5.  $endpoint ="http://localhost:8080/sparql/";
  6.  
  7.      $q "
  8.              PREFIX a: <http://example.com/test/a/>
  9.              PREFIX b: <http://example.com/test/b/>
  10.              DELETE DATA {
  11.                  GRAPH <".$graph."> {
  12.                  a:A b:Name \"Test2\" .
  13.              }}";
  14.  
  15.      $res $sp_write->query($q,'raw');
  16.      $err $sp_write->getErrors();
  17.   if ($err{
  18.          print_r($err);
  19.          throw new Exception(print_r($err,true));
  20.      }
  21.      var_dump($res);
  22.  ?>

You can create or replace a graph with the function set.

Example :

  1.  <?php
  2.   require_once('php4store/Endpoint.php');
  3.  
  4.  $readonly false;
  5.  $s new Endpoint('http://localhost:8080/',$readonly);
  6.  
  7.  $r $s->set('http://example/test'"
  8.      @ prefix foaf: <http://xmlns.com/foaf/0.1/> .
  9.      <http://github.com/bordercloud/4store-php> foaf:maker <http://www.bordercloud.com/wiki/user:Karima_Rafes> . ");
  10.  
  11.  ?>

You can add data (Turtle format) in a graph with the function add.

Example :

  1.  <?php
  2.   require_once('php4store/Endpoint.php');
  3.  
  4.  $readonly false;
  5.  $s new Endpoint('http://localhost:8080/',$readonly);
  6.  $r $s->add('http://example/test'"
  7.      @ prefix foaf: <http://xmlns.com/foaf/0.1/> .
  8.      <http://www.bordercloud.com/wiki/user:Karima_Rafes>  foaf:workplaceHomepage <http://www.bordercloud.com> . ");
  9.  var_dump($r);
  10.  
  11.  ?>

You can delete a graph with the function delete.

Example :

  1.  <?php
  2.   require_once('php4store/Endpoint.php');
  3.  
  4.  $readonly false;
  5.  $s new Endpoint('http://localhost:8080/',$readonly);
  6.  
  7.    $r $s->delete('http://example/test');
  8.    var_dump($r);
  9.  ?>

Located in /php4store/Endpoint.php (line 189)


	
			
Method Summary
 Endpoint __construct (string $endpoint, [boolean $readOnly = true], [boolean $debug = false], [string $proxy_host = null], [string $proxy_port = null])
 boolean add (string $graph, string $turtle)
 boolean check ()
 number count ([string $graph = null])
 boolean delete (string $graph)
 array getErrors ()
 array|boolean query (string $q, [string $result_format = ''])
 string queryRead (string $query, [string $typeOutput = null])
 string queryReadJSON (string $query)
 string queryReadTabSeparated (string $query)
 boolean queryUpdate (string $query)
 boolean set (string $graph, string $turtle)
Methods
Constructor __construct (line 239)

Constructor of Graph

  • access: public
Endpoint __construct (string $endpoint, [boolean $readOnly = true], [boolean $debug = false], [string $proxy_host = null], [string $proxy_port = null])
  • string $endpoint: : url of endpoint, example : http://lod.bordercloud.com/sparql
  • boolean $readOnly: : true by default, if you allow the function query to write in the database
  • boolean $debug: : false by default, set debug to true in order to get usefull output
  • string $proxy_host: : null by default, IP of your proxy
  • string $proxy_port: : null by default, port of your proxy
add (line 319)

Add new data in a graph.

  • return: : true if it did
  • access: public
boolean add (string $graph, string $turtle)
  • string $graph: : name of the graph
  • string $turtle: : list of the triples
check (line 275)

Check if the server is up.

  • return: true if the triplestore is up.
  • access: public
boolean check ()
count (line 436)

Count the number of triples in a graph or in the endpoint.

  • access: public
number count ([string $graph = null])
  • string $graph: : put name of the graph or nothing to count all triples in the endpoint
delete (line 349)

Delete a graph with its data.

  • return: : true if it did
  • access: public
boolean delete (string $graph)
  • string $graph: : name of the graph
getErrors (line 426)

Give the errors

  • access: public
array getErrors ()
query (line 383)

This function parse a SPARQL query, send the query and parse the SPARQL result in a array.

You can custom the result with the parameter $result_format :

  • rows to return array of results
  • row to return array of first result
  • raw to return boolean for request ask, insert and delete

  • return: in function of parameter $result_format
  • access: public
array|boolean query (string $q, [string $result_format = ''])
  • string $q: : Query SPARQL
  • string $result_format: : Optional, rows, row or raw
queryRead (line 462)

Send a request SPARQL of type select or ask to endpoint directly and output the response of server. If you want parse the result of this function, it's better and simpler to use the function query().

if you want use another format, you can use directly the function queryReadJSON and queryReadTabSeparated

  • return: response of server or false if error (to do getErrors())
  • access: public
string queryRead (string $query, [string $typeOutput = null])
  • string $query: : Query Sparql
  • string $typeOutput: by default "application/sparql-results+xml",
queryReadJSON (line 493)

Send a request SPARQL of type select or ask to endpoint directly and output the response

of server in the format JSON

  • return: response of server in the format JSON
  • access: public
string queryReadJSON (string $query)
  • string $query: : Query Sparql
queryReadTabSeparated (line 504)

Send a request SPARQL of type select or ask to endpoint directly and output the response

of server in the format TabSeparated

  • return: response of server in the format TabSeparated
  • access: public
string queryReadTabSeparated (string $query)
  • string $query: : Query Sparql
queryUpdate (line 520)

Send a request SPARQL of type insert data or delete data to endpoint directly.

If you want check the query before to send, it's better to use the function query() in the class StorePlus.

  • Example insert : PREFIX ex: <http://example.com/> INSERT DATA { GRAPH <http://mygraph> { ex:a ex:p 12 .}}
  • Example delete : PREFIX ex: <http://example.com/> DELETE DATA { GRAPH <http://mygraph> { ex:a ex:p 12 .}}

  • return: true if it did or false if error (to do getErrors())
  • access: public
boolean queryUpdate (string $query)
  • string $query: : Query Sparql of type insert data or delete data only
set (line 286)

Create or replace the data in a graph.

  • return: : true if it did
  • access: public
boolean set (string $graph, string $turtle)
  • string $graph: : name of the graph
  • string $turtle: : list of the triples

Documentation generated on Sat, 20 Aug 2011 18:46:06 +0200 by phpDocumentor 1.4.3