* @license LGPL The GNU Lesser GPL (LGPL) or an MIT-like license.
* @see http://www.w3.org/2009/sparql/wiki/Main_Page
* @see http://dbpedia.org
* @see dbpedia.php
* @see musicbrainz.php
* @see http://drupal.org/project/querypath
*/
require '../src/QueryPath/QueryPath.php';
// We are using the dbpedia database to execute a SPARQL query.
// URL to DB Pedia's SPARQL endpoint.
$url = 'http://dbpedia.org/sparql';
// The SPARQL query to run.
$sparql = '
PREFIX foaf:
PREFIX rdfs:
SELECT ?uri ?name ?label
WHERE {
?uri foaf:name ?name .
?uri rdfs:label ?label
FILTER (?name = "The Beatles")
FILTER (lang(?label) = "en")
}
';
// We first set up the parameters that will be sent.
$params = [
'query' => $sparql,
'format' => 'application/sparql-results+xml',
];
// DB Pedia wants a GET query, so we create one.
$data = http_build_query($params);
$url .= '?' . $data;
// Next, we simply retrieve, parse, and output the contents.
$qp = qp($url, 'head');
// Get the headers from the resulting XML.
$headers = [];
foreach ($qp->children('variable') as $col) {
$headers[] = $col->attr('name');
}
// Get rows of data from result.
$rows = [];
$col_count = count($headers);
foreach ($qp->top()->find('results>result') as $row) {
$cols = [];
$row->children();
for ($i = 0; $i < $col_count; ++$i) {
$cols[$i] = $row->branch()->eq($i)->text();
}
$rows[] = $cols;
}
// Turn data into table.
$table = '| ' . implode(' | ', $headers) . ' |
';
foreach ($rows as $row) {
$table .= '| ';
$table .= implode(' | ', $row);
$table .= ' |
';
}
$table .= '
';
// Add table to HTML document.
qp(QueryPath::HTML_STUB, 'body')->append($table)->writeHTML();