Php, curl and SPARQL: turning shell scripts into (simple) dynamic web pages

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.

tool interface 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 &quote; 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.

One response to “Php, curl and SPARQL: turning shell scripts into (simple) dynamic web pages”

Leave a Reply

Latest from Flickr

			<p><a href=\"http://www.flickr.com/people/carlotorniai/\">carlotorniai</a> posted a photo:</p>	<p><a href=\"http://www.flickr.com/photos/carlotorniai/7628216988/\" title=\"\"><img src=\"http://farm9.staticflickr.com/8025/7628216988_e1485e60b8_m.jpg\" width=\"240\" height=\"180\" alt=\"\" /></a></p> 			<p><a href=\"http://www.flickr.com/people/carlotorniai/\">carlotorniai</a> posted a photo:</p>	<p><a href=\"http://www.flickr.com/photos/carlotorniai/7628216560/\" title=\"\"><img src=\"http://farm8.staticflickr.com/7133/7628216560_30e3514748_m.jpg\" width=\"240\" height=\"179\" alt=\"\" /></a></p> 			<p><a href=\"http://www.flickr.com/people/carlotorniai/\">carlotorniai</a> posted a photo:</p>	<p><a href=\"http://www.flickr.com/photos/carlotorniai/7628215950/\" title=\"\"><img src=\"http://farm8.staticflickr.com/7248/7628215950_c67ea3da9a_m.jpg\" width=\"240\" height=\"179\" alt=\"\" /></a></p> 			<p><a href=\"http://www.flickr.com/people/carlotorniai/\">carlotorniai</a> posted a photo:</p>	<p><a href=\"http://www.flickr.com/photos/carlotorniai/7628215288/\" title=\"\"><img src=\"http://farm9.staticflickr.com/8281/7628215288_976b113c6d_m.jpg\" width=\"240\" height=\"179\" alt=\"\" /></a></p> 			<p><a href=\"http://www.flickr.com/people/carlotorniai/\">carlotorniai</a> posted a video:</p>	<p><a href=\"http://www.flickr.com/photos/carlotorniai/7628214924/\" title=\"\"><img src=\"http://farm8.staticflickr.com/7133/7628214924_b522e0af16_m.jpg\" width=\"240\" height=\"135\" alt=\"\" /></a></p> 			<p><a href=\"http://www.flickr.com/people/carlotorniai/\">carlotorniai</a> posted a video:</p>	<p><a href=\"http://www.flickr.com/photos/carlotorniai/7628198058/\" title=\"\"><img src=\"http://farm9.staticflickr.com/8142/7628198058_3f81bba63c_m.jpg\" width=\"240\" height=\"135\" alt=\"\" /></a></p>