如何打印Google Books api说明?

时间:2015-05-14 21:52:57

标签: symfony twig

我正在尝试获取概要和其他项目,如作者和出版日期。但我只能通过某些搜索字词来实现这一点,其他字词或术语会出现错误

  

Key" description"对于带有键的数组" title,subtitle,authors,publishedDate,industryIdentifiers,readingModes,pageCount,printType,categories,maturityRating,allowAnonLogging,contentVersion,imageLinks,language,previewLink,infoLink,canonicalVolumeLink"不存在。

我正在使用symfony和twig。这就是twig文件的样子:

{% for item in items %} <article> <img src="{{ item.volumeInfo.imageLinks.thumbnail}}"/> <h4>{{ item.volumeInfo.title}}</h4> {{ item.volumeInfo.description }} <strong> {{ item.volumeInfo.publishedDate }}</strong><br/> <b>{{ item.volumeInfo.authors | join }}</b> </article>

我做错了什么?为什么这有时只能起作用?我怎样才能让它始终正常工作?

class GoogleBooksController extends Controller
{
public function getVolumeAction($title)
{

    $client =new client();
    $response = $client-         >get("https://www.googleapis.com/books/v1/volumes?q=$title");
    $data=$response->json();

    $items=$data['items'];


    return $this->render('BookReviewBundle:GoogleBooks:volume.html.twig', array('items'=>$items
            // ...
        ));    }

谢谢

1 个答案:

答案 0 :(得分:0)

我相信描述字段不是强制性的,所以你可以按照

进行操作
{% if item.volumeInfo.description is defined %}
{{ item.volumeInfo.description }}
{% endif %}