PHP code example of sweetrdf / sparql-client

1. Go to this page and download the library: Download sweetrdf/sparql-client library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

sweetrdf / sparql-client example snippets



$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);
}


$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);
}

  $connection = new \sparqlClient\StandardConnection(
      'https://query.wikidata.org/sparql', 
      new \quickRdf\DataFactory(),
      new \GuzzleHttp\Client(['auth' => ['login', 'pswd']])
  );