4月 4, 2021 - php开发, 开发笔记    Guzzle client 获取错误码已关闭评论

Guzzle client 获取错误码

如何获取Guzzle client的错误码,比如403,看下面的示例

try {

    $url = 'https://www.youtube.com/channel/'.'UCwprv8E-H9d_djdg6T26nkw2';

    $client = new Client();

    $res = $client->request('GET', $url, [

        'headers' => [

        ],

        'timeout' => 10, //超时时间(秒)

    ]);

    $statusCode = $res->getStatusCode(); // 获得接口反馈状态码

    //$body = $res->getBody();

    dd($statusCode);

} catch (RequestException $e) {

    if ($e->hasResponse()) {

        $response = $e->getResponse();

//will get 404 code

        dd($response->getStatusCode());

    }

}

评论被关闭。