The idea is to use PHP to query SPARQL endpoints and generate HTML pages.
There are several PHP libraries to query SPARQL endpoints. Of course they are all either incomplete or buggy or poorly documented or … . Anyway …
1. Create a sub-directory, say linked-data, in your web site directory.
2. Download the ARC2 library from https://github.com/semsol/arc2/tarball/master (on https://github.com/semsol/arc2/wiki/) into linked-data
2. Decompress the .tar.gz file. It should create a directory called something like semsol-arc2-ebd59b0. Rename it to semsol
3. In the same directory create test1.php with the following content
<html> <body> <?php include_once('semsol/ARC2.php'); /* ARC2 static class inclusion */ $config = array( /* remote endpoint configuration */ ' remote_store_endpoint' => 'http://api.talis.com/stores/ordnance-survey/services/sparql', ); $store = ARC2::getRemoteStore($config); /* instantiation */ /* a query to retrieve the districts and their names */ $query = ' prefix rdf: <http://www.w3.org/2000/01/rdf-schema#> prefix os: <http://data.ordnancesurvey.co.uk/ontology/admingeo/> select distinct ?c ?l where { ?c a os:District . ?c rdf:label ?l} limit 100 '; $rows = $store->query($query, 'rows'); /* execute the query */ /* display the results in an HTML table */ echo "<table border='1'>" ; foreach( $rows as $row ) { /* loop for each returned row */ print "<tr><td>" .$row['c'] . "</td><td>" . $row['l']. "</td></tr>"; } echo "</table>" ?> </body> </html>
4. In a browser open the URL http://your-site/linked-data/test1.php
You should see something like
| http://data.ordnancesurvey.co.uk/id/7000000000010124 | The Borough of North Warwickshire |
| http://data.ordnancesurvey.co.uk/id/7000000000003923 | The Borough of Dacorum |
| http://data.ordnancesurvey.co.uk/id/7000000000003897 | The District of North Hertfordshire |
| http://data.ordnancesurvey.co.uk/id/7000000000003826 | The District of Three Rivers |
| http://data.ordnancesurvey.co.uk/id/7000000000003876 | The City of St Albans |
| http://data.ordnancesurvey.co.uk/id/7000000000003728 | The Borough of Welwyn Hatfield |
| http://data.ordnancesurvey.co.uk/id/7000000000003796 | The Borough of Watford |
| http://data.ordnancesurvey.co.uk/id/7000000000003679 | The District of East Hertfordshire |
| http://data.ordnancesurvey.co.uk/id/7000000000003708 | The Borough of Hertsmere |
| ... | ... |
As we can see on the previous example, the php code to query a remote endpoint must proceed as the follows
When executed, a query of the form
select ?x1 … ?xn where <condition>
yields a set of rows, each one containing value for the xi's that satisfy the selection condition.
The standard way to process the results is through an iteration
foreach $rows as $r { ... <do something with $r> ... }
$r is an associative array, with the following contents: