Intro
This is a very simple RDF library to query SPARQL from PHP. It currently ignores language and datatype information to make it feel as similar as possible to the normal PHP SQL libraries.
- Download: sparqllib.php (LGPL)
If you want to get started really quickly, the following command line will install sparqllib.php. You should run it in the same directory as where your PHP code resides.
curl -s http://graphite.ecs.soton.ac.uk/download.php/sparqllib.php -o sparqllib.php
Or get the latest version from Github.
Also hosted on this site is Graphite, a simple PHP library for querying RDF data.
Classic mysql_query style
The library provides functions very similar to mysql_* for comfort.
Code
<?php require_once( "sparqllib.php" ); $db = sparql_connect( "http://sparql.data.southampton.ac.uk/" ); if( !$db ) { print sparql_errno() . ": " . sparql_error(). "\n"; exit; } sparql_ns( "rooms","http://vocab.deri.ie/rooms#" ); $sparql = "SELECT DISTINCT * WHERE { ?room a rooms:Building . ?room rdfs:label ?label } LIMIT 5"; $result = sparql_query( $sparql ); if( !$result ) { print sparql_errno() . ": " . sparql_error(). "\n"; exit; } $fields = sparql_field_array( $result ); print "<p>Number of rows: ".sparql_num_rows( $result )." results.</p>"; print "<table class='example_table'>"; print "<tr>"; foreach( $fields as $field ) { print "<th>$field</th>"; } print "</tr>"; while( $row = sparql_fetch_array( $result ) ) { print "<tr>"; foreach( $fields as $field ) { print "<td>$row[$field]</td>"; } print "</tr>"; } print "</table>";
Output