Skip to content

Instantly share code, notes, and snippets.

@hubgit hubgit/1a-query.sparql
Last active Dec 31, 2015

Embed
What would you like to do?
SPARQL query: articles published in Nature between 2011-01-01 and 2012-01-01, with genre "Research"
SELECT * WHERE {
?article a <http://ns.nature.com/terms/Article> ;
<http://ns.nature.com/terms/hasPublication> <urn:issn:0028-0836> ;
<http://prismstandard.org/namespaces/basic/2.1/genre> 'Research' ;
<http://purl.org/dc/elements/1.1/title> ?title ;
<http://prismstandard.org/namespaces/basic/2.1/doi> ?doi ;
<http://prismstandard.org/namespaces/basic/2.1/publicationDate> ?date .
FILTER (?date >= "2011-06-15"^^xsd:date)
FILTER (?date < "2012-06-15"^^xsd:date)
}
ORDER BY ASC(?date)
<?php
$query = file_get_contents('query.sparql');
$output = fopen('data/articles.json', 'w');
$curl = curl_init('http://data.nature.com/sparql');
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Accept: application/sparql-results+json'));
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query(array('query' => $query)));
curl_setopt($curl, CURLOPT_FILE, $output);
curl_exec($curl);
<?php
$json = file_get_contents('data/articles.json');
$response = json_decode($json);
$output = fopen('data/articles.csv', 'w');
fputcsv($output, array('doi', 'date', 'title'));
foreach ($response->results->bindings as $item) {
$data = array(
'doi' => $item->doi->value,
'date' => $item->date->value,
'title' => preg_replace('/\s+/', ' ', $item->title->value),
);
fputcsv($output, $data);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.