This repository has been archived by the owner on Dec 1, 2021. It is now read-only.
forked from easyrdf/easyrdf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
basic_sparql.php
56 lines (51 loc) · 1.73 KB
/
basic_sparql.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?php
/**
* Making a SPARQL SELECT query
*
* This example creates a new SPARQL client, pointing at the
* dbpedia.org endpoint. It then makes a SELECT query that
* returns all of the countries in DBpedia along with an
* english label.
*
* Note how the namespace prefix declarations are automatically
* added to the query.
*
* @package EasyRdf
* @copyright Copyright (c) 2009-2020 Nicholas J Humfrey
* @license http://unlicense.org/
*/
require_once realpath(__DIR__.'/..')."/vendor/autoload.php";
require_once __DIR__."/html_tag_helpers.php";
// Setup some additional prefixes for DBpedia
\EasyRdf\RdfNamespace::set('dbc', 'http://dbpedia.org/resource/Category:');
\EasyRdf\RdfNamespace::set('dbpedia', 'http://dbpedia.org/resource/');
\EasyRdf\RdfNamespace::set('dbo', 'http://dbpedia.org/ontology/');
\EasyRdf\RdfNamespace::set('dbp', 'http://dbpedia.org/property/');
$sparql = new \EasyRdf\Sparql\Client('http://dbpedia.org/sparql');
?>
<html>
<head>
<title>EasyRdf Basic Sparql Example</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
</head>
<body>
<h1>EasyRdf Basic Sparql Example</h1>
<h2>List of countries</h2>
<ul>
<?php
$result = $sparql->query(
'SELECT * WHERE {'.
' ?country rdf:type dbo:Country .'.
' ?country rdfs:label ?label .'.
' ?country dct:subject dbc:Member_states_of_the_United_Nations .'.
' FILTER ( lang(?label) = "en" )'.
'} ORDER BY ?label'
);
foreach ($result as $row) {
echo "<li>".link_to($row->label, $row->country)."</li>\n";
}
?>
</ul>
<p>Total number of countries: <?= $result->numRows() ?></p>
</body>
</html>