sparqllib.php

Simple library to query SPARQL from PHP.

©2010-12 Christopher Gutteridge, University of Southampton.

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.

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://rdf.ecs.soton.ac.uk/sparql/" );
if( !$db ) { print sparql_errno() . ": " . sparql_error(). "\n"; exit; }
sparql_ns( "foaf","http://xmlns.com/foaf/0.1/" );
 
$sparql = "SELECT * WHERE { ?person a foaf:Person . ?person foaf:name ?name } 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
403: Bad response, 403: 403 Forbidden

Forbidden

You don't have permission to access /sparql/ on this server.


Apache Server at rdf.ecs.soton.ac.uk Port 80