0

我有一个来自 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列表。

请帮我找出我的代码的错误。谢谢

4

1 回答 1

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'];  
}
于 2019-03-05T11:47:15.300 回答