Last active
December 3, 2018 10:56
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 <[email protected]> | |
# @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