Skip to content

Latest commit

 

History

History
923 lines (527 loc) · 13.9 KB

File metadata and controls

923 lines (527 loc) · 13.9 KB

BorderCloud\SPARQL\SparqlClient

Sparql HTTP Client for SPARQL1.1's Endpoint

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

Example : send a simple query to DBpedia

<?php

use BorderCloud\SPARQL\SparqlClient;

$endpoint = "http://dbpedia.org/sparql";
$sc = new SparqlClient();
$sc->setEndpointRead($endpoint);
$q = "select * where {?x ?y ?z.} LIMIT 5";
$rows = $sc->query($q, 'rows');
$err = $sc->getErrors();
if ($err) {
     print_r($err);
     throw new Exception(print_r($err,true));
}

foreach($rows["result"]["variables"] as $variable){
     printf("%-20.20s",$variable);
     echo '|';
}
echo "\n";

foreach ($rows["result"]["rows"] as $row){
     foreach($rows["result"]["variables"] as $variable){
         printf("%-20.20s",$row[$variable]);
         echo '|';
     }
     echo "\n";
}
?>

For the different server, you can use the property setEndpointQuery, setEndpointUpdate,setNameParameterQueryRead or setNameParameterQueryWrite.

EXAMPLE to config : Virtuoso

$sc = new SparqlClient();
$sc->setEndpointRead($endpoint);
$sc->setEndpointWrite($endpoint);

EXAMPLE to config : Sesame

$sc = new SparqlClient();
$sc->setEndpointRead("http://localhost/openrdf-sesame/repositories/tests");
$sc->setEndpointWrite("http://localhost/openrdf-sesame/repositories/tests/statements");

EXAMPLE to config : Fuseki

$sc = new SparqlClient();
$sc->setEndpointRead("http://localhost/tests/query");
$sc->setEndpointWrite("http://localhost/tests/update");

EXAMPLE to config : Allegrograph

$sc = new SparqlClient();
$sc->setEndpointRead("http://localhost/repositories/tests");
$sc->setEndpointWrite("http://localhost/repositories/tests");
$sc->setNameParameterQueryWrite("query");

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

<?php
$q = "PREFIX a: <http://example.com/test/a/>
PREFIX b: <http://example.com/test/b/>
ask where { GRAPH <".$graph."> {a:A b:Name \"Test3\" .}} ";
$res = $sc->query($q);
$err = $sc->getErrors();
if ($err) {
     print_r($err);
     throw new Exception(print_r($err,true));
}
var_dump($res);
?>

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

$sc = new SparqlClient();
$sc->setEndpointRead($MyEndPointSparql);
$sc->setEndpointWrite($MyEndPointSparql);
echo "\nInsert :";
$q = "
PREFIX a: <http://example.com/test/a/>
PREFIX b: <http://example.com/test/b/>
INSERT DATA {
GRAPH <".$MyGraph."> {
a:A b:Name \"Test1\" .
a:A b:Name \"Test2\" .
a:A b:Name \"Test3\" .
}}";
$res = $sc->query($q,'raw');
$err = $sc->getErrors();
if ($err) {
     print_r($err);
     throw new Exception(print_r($err,true));
}
var_dump($res);

Example : send a query Delete

$sc = new SparqlClient();
$sc->setEndpointRead($MyEndPointSparql);
$sc->setEndpointWrite($MyEndPointSparql);

echo "\nDelete :";
$q = "
PREFIX a: <http://example.com/test/a/>
PREFIX b: <http://example.com/test/b/>
DELETE DATA {
GRAPH <".$MyGraph."> {
a:A b:Name \"Test2\" .
}}";

$res = $sp_write->query($q,'raw');
$err = $sp_write->getErrors();
if ($err) {
     print_r($err);
     throw new Exception(print_r($err,true));
}
var_dump($res);

You can change the format of the response with the function queryRead and queryUpdate.

Extend:

BorderCloud\SPARQL\Base

Methods

Name Description
checkEndpointRead Check if the SPARQL endpoint for reading is up.
checkEndpointWrite Check if the SPARQL endpoint for writing is up.
getEndpointRead Get the url to read
getEndpointWrite Get the url to write
getLastError Get last message for Sparql editor, ie print only the error syntax message.
getLogin Get the server login
getMethodHTTPRead Get the method HTTP to read
getMethodHTTPWrite Get the method HTTP to write
getNameParameterQueryRead Get the parameter in the query to read
getNameParameterQueryWrite Get the parameter in the query to write
getPassword Get the server login
getProxyHost Get the proxy address
getProxyPort Get the proxy port
mtime TODO
query This function parse a SPARQL query, send the query and parse the SPARQL result in a array.
queryRead Send a request SPARQL of type select or ask to endpoint directly and output the response
of server.
queryUpdate Send a request SPARQL of type insert data or delete data to endpoint directly.
setEndpointRead Set the url to read
setEndpointWrite Set the url to write
setLogin Set the server login
setMethodHTTPRead Set the method HTTP to read
setMethodHTTPWrite Set the method HTTP to write
setNameParameterQueryRead Set the parameter in the query to read
setNameParameterQueryWrite Set the parameter in the query to write
setPassword Set the server password
setProxyHost Set the proxy address
setProxyPort Set the proxy port

Inherited methods

Name Description
__construct TODO
addError TODO
getErrors Give the errors
resetErrors TODO

SparqlClient::checkEndpointRead

Description

public checkEndpointRead (void)

Check if the SPARQL endpoint for reading is up.

Parameters

This function has no parameters.

Return Values

bool

true if the service is up.


SparqlClient::checkEndpointWrite

Description

public checkEndpointWrite (void)

Check if the SPARQL endpoint for writing is up.

Parameters

This function has no parameters.

Return Values

bool

true if the service is up.


SparqlClient::getEndpointRead

Description

public getEndpointRead (void)

Get the url to read

Parameters

This function has no parameters.

Return Values

string

$url : endpoint's url to read


SparqlClient::getEndpointWrite

Description

public getEndpointWrite (void)

Get the url to write

Parameters

This function has no parameters.

Return Values

string

$url : endpoint's url to write


SparqlClient::getLastError

Description

public getLastError (void)

Get last message for Sparql editor, ie print only the error syntax message.

Message supported for the moment :

  • Wikidata
  • Virtuoso 7

Parameters

This function has no parameters.

Return Values

string


SparqlClient::getLogin

Description

public getLogin (void)

Get the server login

Parameters

This function has no parameters.

Return Values

string

$login : server login


SparqlClient::getMethodHTTPRead

Description

public getMethodHTTPRead (void)

Get the method HTTP to read

Parameters

This function has no parameters.

Return Values

void


SparqlClient::getMethodHTTPWrite

Description

public getMethodHTTPWrite (void)

Get the method HTTP to write

Parameters

This function has no parameters.

Return Values

void


SparqlClient::getNameParameterQueryRead

Description

public getNameParameterQueryRead (void)

Get the parameter in the query to read

Parameters

This function has no parameters.

Return Values

string

$name : name of parameter


SparqlClient::getNameParameterQueryWrite

Description

public getNameParameterQueryWrite (void)

Get the parameter in the query to write

Parameters

This function has no parameters.

Return Values

string

$name : name of parameter


SparqlClient::getPassword

Description

public getPassword (void)

Get the server login

Parameters

This function has no parameters.

Return Values

string

$password : server password


SparqlClient::getProxyHost

Description

public getProxyHost (void)

Get the proxy address

Parameters

This function has no parameters.

Return Values

string


SparqlClient::getProxyPort

Description

public getProxyPort (void)

Get the proxy port

Parameters

This function has no parameters.

Return Values

int


SparqlClient::mtime

Description

public static mtime (void)

TODO

Parameters

This function has no parameters.

Return Values

float


SparqlClient::query

Description

public query (string $q, string $result_format, int $timeout)

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 bool for request ask, insert and delete

Parameters

  • (string) $q : : Query SPARQL
  • (string) $result_format : : Optional, rows, row or raw
  • (int) $timeout : : Optional, time in seconds for complete query (default 600)

Return Values

array|bool

in function of parameter $result_format


SparqlClient::queryRead

Description

public queryRead (string $query, string $typeOutput, int $timeout)

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().

Parameters

  • (string) $query : : Query Sparql
  • (string) $typeOutput : by default "application/sparql-results+xml",
  • (int) $timeout : : Optional, time in seconds for complete query (default 600)

Return Values

string

response of server or false if error (to do getErrors())


SparqlClient::queryUpdate

Description

public queryUpdate (string $query, string $typeOutput, int $timeout)

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

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 .  
     }  
}  

Parameters

  • (string) $query : : Query Sparql of type insert data or delete data only
  • (string) $typeOutput : by default "application/sparql-results+xml",
  • (int) $timeout : : Optional, time in seconds for complete query (default 600)

Return Values

bool

true if it did or false if error (to do getErrors())


SparqlClient::setEndpointRead

Description

public setEndpointRead (string $url)

Set the url to read

Parameters

  • (string) $url : : endpoint's url to read

Return Values

void


SparqlClient::setEndpointWrite

Description

public setEndpointWrite (string $url)

Set the url to write

Parameters

  • (string) $url : : endpoint's url to write

Return Values

void


SparqlClient::setLogin

Description

public setLogin (string $login)

Set the server login

Parameters

  • (string) $login : : server login

Return Values

void


SparqlClient::setMethodHTTPRead

Description

public setMethodHTTPRead (string $method)

Set the method HTTP to read

Parameters

  • (string) $method : : HTTP method (GET or POST) for reading data (by default is POST)

Return Values

void


SparqlClient::setMethodHTTPWrite

Description

public setMethodHTTPWrite (string $method)

Set the method HTTP to write

Parameters

  • (string) $method : : HTTP method (GET or POST) for writing data (by default is POST)

Return Values

void


SparqlClient::setNameParameterQueryRead

Description

public setNameParameterQueryRead (string $name)

Set the parameter in the query to read

Parameters

  • (string) $name : : name of parameter

Return Values

void


SparqlClient::setNameParameterQueryWrite

Description

public setNameParameterQueryWrite (string $name)

Set the parameter in the query to write

Parameters

  • (string) $name : : name of parameter

Return Values

void


SparqlClient::setPassword

Description

public setPassword (string $password)

Set the server password

Parameters

  • (string) $password : : server password

Return Values

void


SparqlClient::setProxyHost

Description

public setProxyHost (string $proxyHost)

Set the proxy address

Parameters

  • (string) $proxyHost

Return Values

void


SparqlClient::setProxyPort

Description

public setProxyPort (int $proxyPort)

Set the proxy port

Parameters

  • (int) $proxyPort

Return Values

void