我在 Laravel 4 中使用 Guzzle 从另一台服务器返回一些数据,但我无法处理错误 400 错误请求
[status code] 400 [reason phrase] Bad Request
using:
$client->get('http://www.example.com/path/'.$path,
[
'allow_redirects' => true,
'timeout' => 2000
]);
怎么解决呢?
谢谢,
正如Guzzle官方文档中所写:http://guzzle.readthedocs.org/en/latest/quickstart.html http://guzzle.readthedocs.org/en/latest/quickstart.html
如果异常请求选项设置为 true,则会引发 400 级错误的 GuzzleHttp\Exception\ClientException
为了正确处理错误,我将使用以下代码:
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
try {
$response = $client->get(YOUR_URL, [
'connect_timeout' => 10
]);
// Here the code for successful request
} catch (RequestException $e) {
// Catch all 4XX errors
// To catch exactly error 400 use
if ($e->hasResponse()){
if ($e->getResponse()->getStatusCode() == '400') {
echo "Got response 400";
}
}
// You can check for whatever error status code you need
} catch (\Exception $e) {
// There was another exception.
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)