In a recent poll on this site I asked "Do you have, or are you planning to learn, any skills related to Linked Data?". Interestingly 60% of respondents (there were 101 votes) said yes, so I thought I should finally get round to writing up a demonstration app that uses Linked Data to provide the information and jQuery Mobile to provide the looks (and more) for a mobile podcast by subject explorer. The site is written using PHP and was developed quite quickly. Again I will be using the Open University's Linked Data store, but the site could easily be adapted to use other stores, maybe even more than one store. Thanks to the use of jQuery Mobile it would even be possible to take the site and embed it in a thin app on the phone to make it look a bit like a native app. Of course the site is a bit rough and ready and I am sure there are thousands of ways to improve it, so experiment and let me know how you get on in the comments.
The site enables uses to find podcasts by subject. It doesn't have as deep as hierarchy as the official OU podcast web site, though this could be added. The first step is to write a bit of common code. In a file named shared.php I wrote functions for making a web request and converting the results into a PHP object, the method was based on my earlier post An approach to consuming Linked Data with PHP. Notice that there is not a lot of error handling, so before use in a production environment you should add code to handle HTTP errors. The contents of the shared.php file are:
// Perform an HTTP request using CURL
function request($url){
// is curl installed?
if (!function_exists('curl_init')){
die('CURL is not installed!');
}
// get curl handle
$ch= curl_init();
// set request url
curl_setopt($ch, CURLOPT_URL, $url);
// return response, don't print/echo
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
// Send a SPARQL query to the OU endpoint and convert the results into a PHP array
function do_LD_Query($sparql) {
$requestURL = 'http://data.open.ac.uk/query?query='.urlencode($sparql);
$response = request($requestURL);
// container for our data
$data = array();
// initialise SimpleXML object and load it with data
$xml = simplexml_load_string($response);
if ($xml === FALSE) {
return $response;
}
// get the
$results = $xml->results;
// loop through
if (isset($results->result)) {
foreach($results->result as $result) {
$line = array();
foreach ($result->binding as $binding) {
if (isset($binding->uri)) {
$line[(string) $binding["name"]] = (string) $binding->uri;
}
else {
// could pick up xsd data type for right cast
$line[(string) $binding["name"]] = (string) $binding->literal;
}
}
$data[] = $line;
}
}
return $data;
}
?>
The starting page for the app gives the user the chance to pick the subject that they are interested in. Sadly it takes a few seconds to generate so the screen is blank for a little while. A good addition here would be some sort of "Loading..." notification. As with all pages that generate output for the user on this site it uses jQuery Mobile to render the look and feel. Thanks to this wonderful library our web application can easily look half decent, it also gives the user a good experience. A SPARQL query is used to get the subjects and a jQuery Mobile listview is used to render them on the page. Note for convenience I am getting the jQuery files directly from their server, but for a production site you should host your own copy. This protects you against breakages due to changes and upgrades, it is also more polite as you won't be using their bandwidth! The index.php page looks like this:
/**
* Start page for app
* Displays index of subjects
*/
require_once("shared.php");
ob_start("ob_gzhandler"); // compress output
// generate and SPARQL query to find out subjects
$sparql =
"PREFIX rdfs:
PREFIX skos:
SELECT DISTINCT ?subject ?subject_label {
?p
?subject skos:inScheme
OPTIONAL {?subject rdfs:label ?subject_label} }
ORDER BY ?subject_label";
$results = do_LD_Query($sparql);
// generate a list of unique subjects
$subjects = array();
foreach ($results as $result) {
$title = $result['subject_label'];
if (empty($title) ) {
$title = $result['subject'];
}
$subjects[$result['subject']] = $title;
}
?>
OU Podcasts
-
// generate a link for each subject
- %s
foreach ($subjects as $subject => $title) {
printf("
", urlencode($subject), htmlentities($title));
}
?>
You should not host your own copy because
You should not host your own copy because when a user visits site A and requests googles jquery library and then visits site B that also requests googles jquery library the library is either cached on the phone or at the ISP.
The library names are version specific so you will never have the problem of getting a file with breaking changes.
Re: You should not host your copy because
That's a valid argument, however with libraries like this I think you have to be aware of the amount of somebody else's bandwidth you are using, they might not be happy if your app makes a million requests per day for example! Also hosting it yourself can mean you are not relying on someone else to not change the file, removing one possible cause of errors.