我有这段代码,但它没有用。它没有显示任何内容。
<?php
$item = file_get_contents('https://www.googleapis.com/books/v1/volumes?q=Bowling Alone&maxResults=1');
$booktitle = (isset($item->volumeInfo->title) ? $item->volumeInfo->title : false);
$description = (isset($item->volumeInfo->description) ? $item->volumeInfo->description : false);
echo "<b>Title:</b> " . $booktitle;
echo "<b>Description:</b> " . $description;
?>
所以,请帮我解决一下!此外,这是获得更快内容的任何方式吗?非常感谢你!
答案 0 :(得分:0)
使其与url兼容,因此不会有空格。您还需要添加foreach循环,因为需要更多结果。并将json结果解码为对象
$item = file_get_contents('https://www.googleapis.com/books/v1/volumes?q=Bowling%20Alone&maxResults=1');
$item = json_decode($item);
foreach ($item->items as $item) {
$booktitle = (isset($item->volumesInfo->title) ? $item->volumeInfo->title : false);
$description = (isset($item->volumeInfo->description) ? $item->volumeInfo->description : false);
echo "<b>Title:</b> " . $booktitle;
echo "<b>Description:</b> " . $description;
}
答案 1 :(得分:0)
<?php
$item = file_get_contents('https://www.googleapis.com/books/v1/volumes?q=Bowling%20Alone&maxResults=1');
$item = json_decode($item);
$item = reset($item->items);
$booktitle = (isset($item->volumesInfo->title) ? $item->volumeInfo->title : false);
$description = (isset($item->volumeInfo->description) ? $item->volumeInfo->description : false);
echo "<b>Title:</b> " . $booktitle;
echo "<b>Description:</b> " . $description;
?>