Creating web pages from linked data with PHP and SPARQL

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. Creating our first page

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

test1.php
  <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/7000000000010124The Borough of North Warwickshire
http://data.ordnancesurvey.co.uk/id/7000000000003923The Borough of Dacorum
http://data.ordnancesurvey.co.uk/id/7000000000003897The District of North Hertfordshire
http://data.ordnancesurvey.co.uk/id/7000000000003826The District of Three Rivers
http://data.ordnancesurvey.co.uk/id/7000000000003876The City of St Albans
http://data.ordnancesurvey.co.uk/id/7000000000003728The Borough of Welwyn Hatfield
http://data.ordnancesurvey.co.uk/id/7000000000003796The Borough of Watford
http://data.ordnancesurvey.co.uk/id/7000000000003679The District of East Hertfordshire
http://data.ordnancesurvey.co.uk/id/7000000000003708The Borough of Hertsmere
......

General structure of a query page

As we can see on the previous example, the php code to query a remote endpoint must proceed as the follows

  1. configure and “open” the endpoint ($config = … ; ARC2::getRemoteStore($config); )
  2. define the query ($query = …)
  3. execute the query ($rows = $store→query($query, 'rows'); )
  4. process (display) the results (foreach $rows as … )

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:

  • $r['x1'], …, $r['xn'] the values for the query variables ?x1?xn.
  • $r['x1 type'], …, $r['xn type'] the type of each value, either 'uri' or 'literal' or 'bnode' (blank node).
  • $r['x1 datatype'], …, $r['xn datatype'] the datatype of each literal value (empty if not a literal or if the datatype is not explicitly specified).
  • $r['x1 lang'], …, $r['xn lang'] the language of each literal value (empty if not a literal or if the language is not explicitly specified).

Endpoints

Last modified: 2012/07/12 12:27
   
 
Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Share Alike 3.0 Unported