0

I have a short JSON file from URL

https://api.crossref.org/works?query.title=Tuberculosis+drug&filter=type:journal-article,from-print-pub-date:2010,until-print-pub-date:2010&select=DOI&rows=3

I used this code to get total-results and the list of DOI value

$crossref_api_url = 'https://api.crossref.org/works?query.title=Tuberculosis+drug&filter=type:journal-article,from-print-pub-date:2010,until-print-pub-date:2010&select=DOI&rows=2';

$JSON = file_get_contents($crossref_api_url);
$Array = json_decode($JSON, true);          

$items_list = $message->items; 
$totalItems = $message->total-results;  
echo $totalItems;

for($i = 0; $i < count($items_list ); $i++) {
    $doi = $items_list[$i]->items->DOI;
    echo $doi;  
}

The result appears 0. There are no totalItems value, DOI list.

Please help me the find the mistake of my code. Thank you

  • `$message`? Where is it __defined__? – u_mulder Mar 05 '19 at 11:44
  • And the structure you got is `{ "message": { "items": [{ "DOI": "10.1111\/j.1365-2958.2010.07218.x"}`, but you are trying to access message->items->items. – 04FS Mar 05 '19 at 11:46

1 Answers1

0

Correct code:

$crossref_api_url = 'https://api.crossref.org/works?query.title=Tuberculosis+drug&filter=type:journal-article,from-print-pub-date:2010,until-print-pub-date:2010&select=DOI&rows=2';

$JSON = file_get_contents($crossref_api_url);
$Array = json_decode($JSON, true);          // with `true` you decode to ARRAY

$items_list = $Array['message']['items']; 
$totalItems = $Array['message']['total-results'];  
echo $totalItems;

foreach ($items_list as $item) {
    echo $item['DOI'];  
}
u_mulder
  • 54,101
  • 5
  • 48
  • 64