[英]Guzzle: handle 400 bad request
我在 Laravel 4 中使用 Guzzle 從另一台服務器返回一些數據,但我無法處理錯誤 400 錯誤請求
[status code] 400 [reason phrase] Bad Request
使用:
$client->get('http://www.example.com/path/'.$path,
[
'allow_redirects' => true,
'timeout' => 2000
]);
如何解決? 謝謝,
正如 Guzzle 官方文檔中所寫: 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.
}
$client->get('http://www.example.com/path/'.$path,
[
'allow_redirects' => true,
'timeout' => 2000,
'http_errors' => true
]);
在請求中使用 http_errors => false 選項。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.