A SPARQL client library for the rdfInterface ecosystem with the API inspired by the PDO.
composer require sweetrdf/sparql-client
composer require guzzlehttp/guzzle
to install an HTTP client (but you can use any client supporting PSR-18).composer require http-interop/http-factory-guzzle
to install PSR-17 bindinds for Guzzle (but you can use any PSR-17 implementation)composer require sweetrdf/quick-rdf
to install RDF terms factory
(you can use any terms factory compatible with the rdfInterface).https://sweetrdf.github.io/sparqlClient/namespaces/sparqlclient.html
It's very incomplete but better than nothing.\ RdfInterface documentation is included which provides documentation for terms (objects representing RDF named nodes, literals, etc.).
include 'vendor/autoload.php';
$connection = new \sparqlClient\StandardConnection('https://query.wikidata.org/sparql', new \quickRdf\DataFactory());
$results = $connection->query('select * where {?a ?b ?c} limit 10');
foreach ($results as $i) {
print_r($i);
}
The StandardConnection
class provides a PDO-like API for parameterized queries (aka prepared statements).
Parameterized queries:
include 'vendor/autoload.php';
$factory = new \quickRdf\DataFactory();
$connection = new \sparqlClient\StandardConnection('https://query.wikidata.org/sparql', $factory);
// pass parameters using execute()
// the single `?` is a positional parameter while the `:sf` is a named parameter
$query = $connection->prepare('SELECT * WHERE {?a ? ?c . ?a :sf ?d .} LIMIT 10');
$query->execute([
$factory::namedNode('http://creativecommons.org/ns#license'),
'sf' => $factory::namedNode('http://schema.org/softwareVersion'),
]);
foreach ($query as $i) {
print_r($i);
}
// bind a (positional) parameter to a variable
$query = $connection->prepare('SELECT * WHERE {?a ? ?c .} LIMIT 2');
$value = $factory::namedNode('http://creativecommons.org/ns#license');
$query->bindParam(0, $value);
$query->execute();
foreach ($query as $i) {
print_r($i);
}
$value = $factory::namedNode('http://schema.org/softwareVersion');
$query->execute();
foreach ($query as $i) {
print_r($i);
}
There are some differences comparing to the PDO API:
bindParam()
/bindValue()
and passing (some or all) parameters trough execute()
is allowed.
execute()
overwrite ones set with bindParam()
/bindValue()
.rdfInterface\Term
.
bindParam()
/bindvalue()
.\sparqlClient\StandardConnection
constructor.
E.g. let's assume your SPARQL endpoint requires authorization and you want to benefit from Guzzle connection allowing to set global request options:
$connection = new \sparqlClient\StandardConnection(
'https://query.wikidata.org/sparql',
new \quickRdf\DataFactory(),
new \GuzzleHttp\Client(['auth' => ['login', 'pswd']])
);
query
request parameter,
you may use the \sparqlClient\Connection
class which takes PSR-7 requests instead of a query string and allows you to use any specific HTTP request you need.