【问题标题】:GuzzleHttp Client class not found in symfony2在 symfony2 中找不到 GuzzleHttp 客户端类
【发布时间】:2023-09-04 10:01:01
【问题描述】:

我已经从

加载了 GuzzleHttp

http://docs.guzzlephp.org/en/5.3/quickstart.html

并拥有

use GuzzleHttp\Client;

当我调用这个动作时......

public function googlevolumeAction(Request $request)
{
    $data = $request->request->all();
    $searchStr = $data['search'];

        $client = new Client();
        $req = $client->request('GET', 'https://www.googleapis.com/books/v1/volumes?q=intitle:' .$searchStr, []);
        $decode = json_decode($req->getBody());
        $total = $decode->totalItems;
        $search = null;

        if ($total != 0) {
            $search = $decode->items;
        }

        return $this->render('BloggerBlogBundle:Page:googlevolume.html.twig',
            ['items' => $search]);
}

我收到此错误...

Attempted to load class "Client" from namespace "GuzzleHttp".
Did you forget a "use" statement for e.g. "Guzzle\Http\Client",     
"Guzzle\Service\Client", "Symfony\Component\BrowserKit\Client", 
"Symfony\Component\HttpKernel\Client" or 
"Symfony\Bundle\FrameworkBundle\Client"?

有什么想法吗?

谢谢

【问题讨论】:

  • 你是如何在你的应用程序中安装它的?
  • 是的,在 composer.json 我有 "require" "guzzlehttp/guzzle": "^3.8" 并且已经更新了它和它在供应商文件夹中
  • 我认为您可能安装了旧版本的 guzzle。我有 "guzzlehttp/guzzle": "^6.2" 也许检查供应商下的客户端类以查看它的名称空间。在早期版本中有所不同。并无视关于使用 \Guzzle 的评论......根本不需要前导反斜杠。是的,guzzle 3.x 已经很老了。如果您确实需要使用它,请遵循相应的文档。

标签: symfony http guzzle


【解决方案1】:

看起来您安装的 guzzle 版本与您正在查看的文档不同。从您收到的错误消息看来,如果您将 use 语句更改为:

use Guzzle\Http\Client;

它应该可以工作。

【讨论】: