Class Endpoint

Description

Sparql HTTP Client for BorderCloud's 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.  
  3.  require_once('bordercloud/Endpoint.php');
  4.  
  5.      $endpoint ="http://dbpedia.org/";
  6.      $sp_readonly new Endpoint($endpoint);
  7.   $q "select *  where {?x ?y ?z.} LIMIT 5";
  8.   $rows $sp_readonly->query($q'rows');
  9.   $err $sp_readonly->getErrors();
  10.   if ($err{
  11.        print_r($err);
  12.        throw new Exception(print_r($err,true));
  13.      }
  14.      var_dump($rows);
  15.  ?>

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

You can insert data also with SPARQL and the function query in your graphs. The BorderCloud's service can host your graphs ( http://www.bordercloud.com ). You can choose your graph's name and Bordercloud will give you a code. With 3 parameters, you are alone to update your graph.

Example : send a query Insert

  1.  <?php
  2.  require_once('bordercloud/Endpoint.php');
  3.  
  4.  //Put here the URL of bordercloud's endpoint
  5.  $MyEndPointSparql "http://lod.bordercloud.com/";
  6.  
  7.  //Put the name of your graph
  8.  $MyGraph "http://FIRSTNAME.me/test"//example
  9.  
  10.  //Put your code Bordercloud
  11.  $MyCode "00000000010";
  12.  
  13.      $sp_write new Endpoint($MyEndPointSparql,$MyCode,$MyGraph);
  14.      echo "\nInsert :";
  15.      $q "
  16.              PREFIX a: <http://example.com/test/a/>
  17.              PREFIX b: <http://example.com/test/b/>
  18.              INSERT DATA {
  19.                  GRAPH <".$MyGraph."> {
  20.                  a:A b:Name \"Test1\" .
  21.                  a:A b:Name \"Test2\" .
  22.                  a:A b:Name \"Test3\" .
  23.              }}";
  24.      $res $sp_write->query($q,'raw');
  25.      $err $sp_write->getErrors();
  26.      if ($err{
  27.          print_r($err);
  28.          throw new Exception(print_r($err,true));
  29.      }
  30.      var_dump($res);
  31.  
  32.  ?>

Example : send a query Delete

  1.  <?php
  2.  require_once('bordercloud/Endpoint.php');
  3.  
  4.  //Put here the URL of bordercloud's endpoint
  5.  $MyEndPointSparql "http://lod.bordercloud.com/";
  6.  
  7.  //Put the name of your graph
  8.  $MyGraph "http://FIRSTNAME.me/test"//example
  9.  
  10.  //Put your code Bordercloud
  11.  $MyCode "00000000010";
  12.  
  13.      $sp_write new Endpoint($MyEndPointSparql,$MyCode,$MyGraph);
  14.  
  15.      echo "\nDelete :";
  16.      $q "
  17.              PREFIX a: <http://example.com/test/a/>
  18.              PREFIX b: <http://example.com/test/b/>
  19.              DELETE DATA {
  20.                  GRAPH <".$MyGraph."> {
  21.                  a:A b:Name \"Test2\" .
  22.              }}";
  23.  
  24.      $res $sp_write->query($q,'raw');
  25.      $err $sp_write->getErrors();
  26.      if ($err{
  27.          print_r($err);
  28.          throw new Exception(print_r($err,true));
  29.      }
  30.      var_dump($res);
  31.  ?>

You can import a file of type rdfxml, ntriples or turtle in your graph.

Example : Push a file RDF

  1.  <?php
  2.  
  3.  //Put here the URL of bordercloud's endpoint
  4.  $MyEndPointSparql "http://lod.bordercloud.com/";
  5.  
  6.  //Put the name of your graph
  7.  $MyGraph "http://FIRSTNAME.me/test"//example
  8.  
  9.  //Put your code Bordercloud
  10.  $MyCode "00000000010";
  11.  
  12.      $s new Endpoint($MyEndPointSparql,$MyCode,$MyGraph);
  13.  
  14.       $filedirname(__FILE__'/../examples/foaf.rdf';
  15.         $codefile $s->importFile($file"rdfxml"$append true;
  16.  
  17.         while($s->isFileWaitToParse($codefile)){
  18.             echo "Waiting 10s.";
  19.             sleep(10);
  20.         }
  21.         $err $s->getErrors();
  22.         if ($err{
  23.             print_r($err);
  24.             $this->assertTrue(false);
  25.         }
  26.  
  27.  ?>

Located in /bordercloud/Endpoint.php (line 177)


	
			
Method Summary
 Endpoint __construct (string $endpoint, [string $jeton = null], [string $graph = null], [boolean $debug = false], [ $proxy_host = null], [ $proxy_port = null])
 boolean check ()
 number count ([string $graph = null])
 array getErrors ()
 string importFile (string $filename, [string $typefile = "rdfxml"], [boolean $append = true])
 boolean isFileWaitToParse (string $codefile)
 array|boolean query (string $q, [string $result_format = ''])
 string queryRead (string $query, [string $typeOutput = "application/sparql-results+xml"])
 string queryReadJSON (string $query)
 string queryReadTabSeparated (string $query)
 boolean queryUpdate (string $query)
 boolean refreshInfo ()
 boolean resetGraph ()
 boolean saveAs (string $file, [string $typefile = "ntriples"])
 number size (string $graph)
Methods
Constructor __construct (line 268)

Constructor of Graph

  • access: public
Endpoint __construct (string $endpoint, [string $jeton = null], [string $graph = null], [boolean $debug = false], [ $proxy_host = null], [ $proxy_port = null])
  • string $endpoint: : url of endpoint, example : http://lod.bordercloud.com/sparql
  • string $jeton: : number give by Bordercloud if you are hosting your graph in the cloud
  • string $graph: : name of the graph in the cloud by default uses in the class
  • boolean $debug: : false by default, set debug to true in order to get usefull output
  • $proxy_host
  • $proxy_port
check (line 320)

Check if the server is up.

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

Give the number of triples in a graph

  • access: public
number count ([string $graph = null])
  • string $graph: : put name of the graph or nothing for the default graph
getErrors (line 380)

Give the errors

  • access: public
array getErrors ()
importFile (line 677)

Import a file in your graph.

The file can have the format :

  • NTriples ( ntriples )
  • RDF ( rdfxml )
  • Turtle ( turtle )

  • return: the code of this file on the server if OK else return false (see errors with getErrors())
  • access: public
string importFile (string $filename, [string $typefile = "rdfxml"], [boolean $append = true])
  • string $filename
  • string $typefile: : optional, rdfxml by default or ntriples or turtle
  • boolean $append: : optional, true by default. if true, you append the new triples in the graph. if false, you if false, you erase the graph before to add the new triples.
isFileWaitToParse (line 710)

Import a file of type rdfxml, ntriples or turtle

  • return: true if the file is waiting on the server else false (check if errors with getErrors())
  • access: public
boolean isFileWaitToParse (string $codefile)
  • string $codefile: this code returned by the function importFile()
query (line 337)

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 507)

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 = "application/sparql-results+xml"])
  • string $query: : Query Sparql
  • string $typeOutput: by default "application/sparql-results+xml"
queryReadJSON (line 539)

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 550)

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 566)

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
refreshInfo (line 389)

Refresh the information of this graph in the graph http://www.bordercloud.com/dataset

  • access: public
boolean refreshInfo ()
resetGraph (line 411)

Reset the graph, delete all the triples in the graph

  • access: public
boolean resetGraph ()
saveAs (line 738)

Save the graph in a file

  • return: true if it's ok
  • access: public
boolean saveAs (string $file, [string $typefile = "ntriples"])
  • string $file
  • string $typefile: optional, ntriples by default to export a file NTriples (only for the moment)
size (line 469)

Give the size of graph (Bytes)

  • access: public
number size (string $graph)
  • string $graph: : put name of the graph or nothing for the default graph

Documentation generated on Fri, 19 Aug 2011 16:45:09 +0200 by phpDocumentor 1.4.3