【问题标题】:Symfony2 Google Books APISymfony2 谷歌图书 API
【发布时间】:2016-05-10 19:42:20
【问题描述】:

我在从 Symfony2 中的 Google Books API 获取图书时遇到问题

我正在为页面 (/google) 发布此表单...

    <form action="/googlevolume" method="post"> 
        <input type="text" name="title" id="title" size="40" value=""> 
        <input type="submit" value="Search">
    </form>

这是我的结果页面控制器 (/googlevolume)...

public function googlevolume(Request $request)
{

        $enquiry = new Enquiry();

        $form->bind($request);

        $response = $enquiry->get("https://www.googleapis.com/books/v1/volumes?q=".$form->get('title')->getData());

        $data=$response->json();

        $response2=$data['items'];

        return $this->render('BloggerBlogBundle:Page:googlevolume.html.twig', array('items' => $response2));


}

我已尝试从表单中发布此号码

1781100489

这与去: https://www.googleapis.com/books/v1/volumes?q=1781100489

但是,当我将该数字输入表单并按搜索时,我收到此错误

Controller "Blogger\BlogBundle\Controller\PageController::googlevolumeAction" for URI "/googlevolume" is not callable.

这是来自我的路由文件...

google:
    pattern: /google
    defaults: { _controller: BloggerBlogBundle:Page:google }
    requirements:
         _method: GET

googlevolume:
    pattern: /googlevolume
    defaults: { _controller: BloggerBlogBundle:Page:googlevolume }
    requirements:
        _method: POST

这是 googlevolume.html.twig ...

 {# src/Blogger/BlogBundle/Resources/views/Page/googlevolume.html.twig #}
 {% extends 'BloggerBlogBundle::layout.html.twig' %}
 {% block title %} Google Books{% endblock%}
 {% block body %}
 <header>
    <h1>Google Book</h1>
 </header>
 <br>
 {% for item in items %}
 <article>
 <img src="{{ item.volumeInfo.imageLinks.thumbnail}}"/>
 <h4>{{ item.volumeInfo.title}}</h4>
 {% if item.volumeInfo.description is defined %}
 {{ item.volumeInfo.description }}
 {% endif %}
 <strong> {{ item.volumeInfo.publishedDate }}</strong><br/>
 <b>{{ item.volumeInfo.authors | join }}</b>
 </article>
 {% endblock %}

有人知道我在哪里做错了吗?

谢谢

【问题讨论】:

    标签: php api symfony google-books


    【解决方案1】:

    看起来你正在尝试序列化请求对象,而实际上你只是想要表单中的值。尝试将您对 api 的请求更改为:

    use Symfony\Component\HttpFoundation\Request;
    use Symfony\Component\HttpFoundation\Response;
    
    ...
    
    public function googlevolumeAction(Request $request)
    {
        $form = $this->createFormBuilder(null,[
                'csrf_protection' => false
            ])
            ->add('title','text')
            ->add('Search', 'submit')
            ->getForm();
    
        $form->bind($request);
        if($form->isValid()){
            $enquiry = new Enquiry();
            $response = json_decode(file_get_contents("https://www.googleapis.com/books/v1/volumes?q=".$form->get('title')->getData()),true);
    
            if(array_key_exists('items',$response)){
                return $this->render('BloggerBlogBundle:Page:googlevolume.html.twig', [
                    'items' => $response['items']
                ]);
            } else {
                return new Response('Google Volume did not have any items', 400);
            }
        }
    
        return new Response('Google Volume Not Found', 404);
    }
    

    然后是路线:

    googlevolume:
        pattern: /googlevolume
        defaults: { _controller: BloggerBlogBundle:Page:googlevolumeAction }
        requirements:
            _method: POST
    

    然后清除缓存:

    php app/console cache:clear --env=prod
    

    或仅用于开发

    php app/console cache:clear
    

    【讨论】:

    • 我需要更改代码的任何其他部分吗?当我将其更改为第一个时,我仍然收到控制器错误
    • 谢谢,这在一定程度上已经解决了……但是,现在我明白了…… Class Blogger\BlogBu​​ndle\Controller\Request 不存在
    • 当我输入数字 1781100489 时,我得到“未找到 Google 卷”。 iv 将 googlevolume.html.twig 添加到问题中
    • 这意味着您的表单有错误。调试尝试将最后一行更改为return new Response('Google Volume Not Found: '.$form-&gt;getErrors(true), 404);
    • CSRF 令牌无效
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-11-24
    • 1970-01-01
    • 2016-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多