Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@SimonXIX
Last active December 3, 2018 10:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SimonXIX/465793c60ab9bad84be67bd1b164726d to your computer and use it in GitHub Desktop.
Save SimonXIX/465793c60ab9bad84be67bd1b164726d to your computer and use it in GitHub Desktop.
Script to check a text file of DOIs (one per line with Unix EOLs) against the Crossref API
<?php
# @name: crossref_doi_checker.php
# @version: 0.1
# @license: The MIT License (MIT) <https://mit-license.org/>
# @purpose: Checks a single text file of DOIs (one per line with Unix EOLs) against the Crossref API.
# Run as 'php crossref_doi_checker.php '$doi_file'' e.g. 'php crossref_doi_checker.php doi_file.txt'
# @author: Simon Bowie <simonxix@riseup.net>
# @acknowledgements:
# https://github.com/CrossRef/rest-api-doc
?>
<?php
# PHP to define variables
$apiurl="https://api.crossref.org/works/";
$doi_file = $argv[1];
#$doi = $argv[1];
$handle = fopen($doi_file, "r");
if ($handle) {
while (($line = fgets($handle)) !== false) {
$line = str_replace("\n", '', $line);
# submit via cURL to Crossref's 'works' function in the API
$ch = curl_init();
$url = $apiurl;
$queryParams = $line . '/';
curl_setopt($ch, CURLOPT_URL, $url . $queryParams);
#curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$response = curl_exec($ch);
#$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
$json_item = json_decode($response);
$title = $json_item->message->title[0];
$issn = $json_item->message->ISSN[0];
echo "$title\n";
echo "$issn\n";
}
fclose($handle);
}
else {
// error opening the file.
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment