从JSON URL获取DOI值列表

时间:2019-03-05 11:40:31

标签: php json parsing doi

我有一个来自URL的简短JSON文件

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

我使用此代码来获取total-results和值DOI的列表

$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;  
}

结果显示为0。没有totalItems值,DOI列表。

请帮助我查找代码错误。谢谢

1 个答案:

答案 0 :(得分:0)

正确的代码:

$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'];  
}