求助帖,请求 API 后怎么获取返回的 http 状态码

直接上问题:

$result = $client->push()
    ->setPlatform('all')
    ->addAllAudience()
    ->setNotificationAlert('Hi, JPush')
    ->send()

上面的代码调用了极光推送的推送代码,他的正确的返回信息是这个

HTTP/1.1 200 OK
{
    "time_unit":"DAY",
    "start":"2014-06-10",
    "duration":3,
    "items":[{"time":"2014-06-10"},
                {"time":"2014-06-11","android":{"active":1}},
                {"time":"2014-06-12","android":{"active":1,"online":2}}]
}

错误的返回信息是这个

 HTTP/1.1 401 Unauthorized
 Content-Type: application/json

{ 
  "error": {
        "code": 3001, 
        "message": "Basic authentication failed"
     }
}

问题来了,我该怎么获取到http状态码呢,我使用的框架是 laravel

《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
讨论数量: 2
Summer

用的这个包吗 https://github.com/jpush/jpush-api-php-cli...

打印下:

dd($result);

被封装起来了,取不到,最终返回时这段调用 https://github.com/jpush/jpush-api-php-cli...

    private function __processResp($response) {
        if($response['http_code'] === 200) {
            $body = array();
            $body['data'] = json_decode($response['body']);
            $headers = $response['headers'];
            if (is_array($headers)) {
                $limit = array();
                foreach (self::$LIMIT_KEYS as $key => $value) {
                    if (array_key_exists($key, $headers)) {
                        $limit[$value] = $headers[$key];
                    }
                }
                if (count($limit) > 0) {
                    $body['limit'] = (object)$limit;
                }
                return (object)$body;
            }
            return $body;
        } else {
            throw new APIRequestException($response);
        }
    }

所以,https://github.com/jpush/jpush-api-php-cli...

try {
    $result = $client->push()
    ->setPlatform('all')
    ->addAllAudience()
    ->setNotificationAlert('Hi, JPush')
    ->send()
    // 正常返回的情况
    dd($result);
} catch (APIRequestException $e) {
    echo "状态码:" . $e->httpCode ."<br>";
    dd($e);
}
7年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!