Skip to content
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
Cannot retrieve contributors at this time
93 lines (82 sloc) 3.42 KB
* Consuming Linked Data from Wikidata.
* This example demonstrates fetching information about villages in Fife
* from Wikidata. The list of villages is fetched by running a SPARQL query.
* If you click on an village, then it fetched by getting the Turtle formatted
* RDF from Wikidata for that village. It then parses the result and
* displays a page about that village with a title, synopsis and Open Street Map.
* @package EasyRdf
* @copyright Copyright (c) 2009-2020 Nicholas J Humfrey
* @license
require_once realpath(__DIR__.'/..')."/vendor/autoload.php";
require_once __DIR__."/html_tag_helpers.php";
// Setup some additional prefixes for Wikidata
\EasyRdf\RdfNamespace::set('wd', '');
\EasyRdf\RdfNamespace::set('wds', '');
\EasyRdf\RdfNamespace::set('wdt', '');
\EasyRdf\RdfNamespace::set('p', '');
\EasyRdf\RdfNamespace::set('wikibase', '');
// SPARQL Query to get a list of villages in Fife
SELECT ?item ?itemLabel
?item wdt:P31 wd:Q532 . # Instance of Village
?item wdt:P131 wd:Q201149 . # Located in Fife
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
ORDER BY ?itemLabel
$WIKIDATA_IMAGE = 'wdt:P18';
$WIKIDATA_POINT = 'wdt:P625';
<head><title>EasyRdf Village Info Example</title></head>
<h1>EasyRdf Village Info Example</h1>
if (isset($_REQUEST['id'])) {
$id = $_REQUEST['id'];
$graph = \EasyRdf\Graph::newAndLoad("wd:$id", 'turtle');
$village = $graph->resource("wd:$id");
if ($village->get($WIKIDATA_IMAGE)) {
print image_tag(
print content_tag('h2',$village->label('en'));
print content_tag('p', $village->get('schema:description', null, 'en'));
if (preg_match("/Point\((\S+) (\S+)\)/", $village->get($WIKIDATA_POINT), $matches)) {
$long = $matches[1];
$lat = $matches[2];
print "<iframe width='420' height='350' frameborder='0' scrolling='no' marginheight='0' marginwidth='0' src='$lat&lon=$long&zoom=14' style='border: 1px solid black'></iframe>";
print content_tag('h3', "Pages about " . $village->label('en'));
print "<ul>\n";
foreach ($graph->all($village, "^schema:about") as $doc) {
print '<li>'.link_to($doc)."</li>\n";
print "</ul>\n";
echo "<br /><br />";
echo $village->dump();
} else {
print "<p>List of villages in Fife.</p>";
$sparql = new \EasyRdf\Sparql\Client($SPARQL_ENDPOINT);
$results = $sparql->query($SPARQL_QUERY);
print "<ul>\n";
foreach ($results as $row) {
if (preg_match("|/(Q\d+)|", $row->item, $matches)) {
print '<li>'.link_to_self($row->itemLabel, "id=".$matches[1])."</li>\n";
print "</ul>\n";