【问题标题】:Fatal error: Call to undefined method GuzzleHttp\Client::request() with Guzzle 6致命错误:使用 Guzzle 6 调用未定义的方法 GuzzleHttp\Client::request()
【发布时间】:2016-05-16 03:13:00
【问题描述】:

我正在使用 Guzzle 6 和 Laravel 5.2。

我正在尝试访问一个简单的内部 API:

use GuzzleHttp\Client;

        $client = new Client(['base_uri' => getenv('URL_BASE').'api/v1/']);
        $response = $client->request('GET', 'tournaments');

我收到这条消息:

Fatal error: Call to undefined method GuzzleHttp\Client::request()

当我看到docs 时,它说:

$client = new GuzzleHttp\Client(['base_uri' => 'https://foo.com/api/']);

但是 PHPStorm 无法解析 GuzzleHttp

我应该怎么做才能让它工作???

【问题讨论】:

  • 这是 Guzze 6 的文档(我猜?),您需要 5 的文档 github.com/guzzle/guzzle/blob/5.3/docs/quickstart.rst
  • 我能够安装 Guzzle 6,但问题仍然存在
  • 你能在 PHPStorm 中看到 Client 类吗?也许需要composer updatecomposer install
  • 如果我删除 GuzzleHttp\,自动补全给了我两个选择:使用 GuzzleHttp\Client 和使用 Guzzle\Http\Client;但它们都不起作用

标签: php laravel guzzle laravel-5.2


【解决方案1】:

我也在使用 guzzle,它对我有用,试试这样

use GuzzleHttp;
use GuzzleHttp\Subscriber\Oauth\Oauth1;

$client = new GuzzleHttp\Client();

要得到响应,试试这个

$response = $client->request('GET', 'tournaments',['query' => ['base_uri' => getenv('URL_BASE').'api/v1/']]);

如果不行就试试这个

$response = $client->request('GET', getenv('URL_BASE').'api/v1/tournaments');

【讨论】:

  • 这确实有效,它实际上基于您使用的 Guzzle 版本。
  • 确保“包含”或“要求” vendor/autoload.php 文件。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-11-20
  • 2015-01-28
  • 2015-05-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多