【发布时间】:2018-10-12 14:15:39
【问题描述】:
Guzzle/Http 新手。
我有一个 API rest url 登录,如果未授权,则返回 401 代码,如果缺少值,则返回 400。
我会获取http状态码来检查是否有问题,但不能只有代码(整数或字符串)。
这是我的一段代码,我在这里确实使用了指令(http://docs.guzzlephp.org/en/stable/quickstart.html#exceptions)
namespace controllers;
use GuzzleHttp\Psr7;
use GuzzleHttp\Exception\ClientException;
$client = new \GuzzleHttp\Client();
$url = $this->getBaseDomain().'/api/v1/login';
try {
$res = $client->request('POST', $url, [
'form_params' => [
'username' => 'abc',
'password' => '123'
]
]);
} catch (ClientException $e) {
//echo Psr7\str($e->getRequest());
echo Psr7\str($e->getResponse());
}
【问题讨论】:
标签: guzzle http-status-codes guzzle6