【问题标题】:Call to undefined method GuzzleHttp\Psr7\Response::isSuccessful()调用未定义的方法 GuzzleHttp\Psr7\Response::isSuccessful()
【发布时间】:2017-08-16 21:04:02
【问题描述】:

所以我已经按照TeamUp calendar documentation安装了Guzzle库版本6。但是,当我尝试运行下面的代码时,我得到了

Fatal error: Call to undefined method GuzzleHttp\Psr7\Response::isSuccessful()  

代码:

<?php
include 'vendor/autoload.php';

define('API_KEY','****ww9d5ea2b0540ba1e02c08100b0e5**');

$client = new GuzzleHttp\Client(['headers' => ['Teamup-Token' => API_KEY]]);
$res = $client->get('https://api.teamup.com/ks************/events?startDate=2016-08-21&endDate=2016-08-25');

if ($res->isSuccessful()) {
    echo $res->getBody();
    // {"event":{ ... }}
}

不应该包含在库中吗? 有人吗?

【问题讨论】:

    标签: php response guzzle psr-7


    【解决方案1】:

    是的,没有方法isSuccessful。 默认情况下,如果服务器返回错误,Guzzle 会抛出异常

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

    500 级错误会引发 GuzzleHttp\Exception\ServerException 如果 http_errors 请求选项设置为 true。

    400 级错误会引发 GuzzleHttp\Exception\ClientException 如果 http_errors 请求选项设置为 true。

    如果发生网络错误(连接超时、DNS 错误、 等),一个 GuzzleHttp\Exception\RequestException 被抛出。

    无论如何,您可以使用检查响应的状态代码

    $res->getStatusCode();
    

    【讨论】:

      【解决方案2】:

      upgrade notes from Guzzle 5.0 to Guzzle 6.0 say

      GuzzleHttp\Message\Response::isSuccessful() 等相关方法已被移除。请改用 getStatusCode()。

      【讨论】:

        猜你喜欢
        • 2015-11-20
        • 1970-01-01
        • 1970-01-01
        • 2020-06-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-09-21
        相关资源
        最近更新 更多