We have SPARQL endpoints requiring http authentication and for my own needs I usually write shell scripts using curl to execute SPARQL queries and to save results in files to be handed to the team responsible for data QA.
I wanted to provide some simple tool for data QA folks (which have no technical knowledge of RDF, OWL or SPARQL whatsoever) to allow them to select and run a set of precompiled SPARQL queries without my intervention.
Since most of my script are using cURL I just decided to develop php pages using cURL to access the SPARQL service.
The process was pretty straightforward: I just had to enable curl form the php.ini and experiment a little :-)
The only issue I encountered was related to the encoding of double quotes included in some SPARQL queries posted from an HTML form on my godaddy server. It took me a while to realize that that was the issue since on my local Apache (working on MAX OS) everything worked fine. I've solved the issue with magic quotes and using the "e; in the form field.
Below the php code that gets the parameters posted form an html form (username, password, institution, query_value) to access the proper SPARQL service.
<?php
// test for double quotes
if (get_magic_quotes_gpc()) {
function stripslashes_gpc(&$value)
{
$value = stripslashes($value);
}
array_walk_recursive($_GET, 'stripslashes_gpc');
array_walk_recursive($_POST, 'stripslashes_gpc');
array_walk_recursive($_COOKIE, 'stripslashes_gpc');
array_walk_recursive($_REQUEST, 'stripslashes_gpc');
}
//extract data from the post
extract($_POST);
//get parameters values
$institution = $_GET['institution'];
$url = 'https://'.$institution.'sparql.service.url;
$view_value='user';http://eagle-i.net/repository/sparql
$format_value='text/html';
$query_value=$_GET['query_value'];
$user=$_GET['user'];
$password=$_GET['password'];
$auth = $user.':'.$password;
$fields = array(
'view'=>urlencode($view_value),
'query'=>urlencode($query_value),
'format'=>urlencode($format_value)
);
//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string,'&');
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_USERPWD, $auth);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
?>
A side note: I went trough this awesome tutorial for enabling SAPRQL queries from Drupal using ARC2 and a bunch of Drupal modules. I made it work but that was way harder then the php scripting ;-) Few more notes about this sometimes soon.






Consider I spent very few time on putting the php part together.. This post is intended more as “notes” for future development. I hope to have time to post some notes with the Drupal part.