安裝guzzle
composer require guzzlehttp/guzzle使用代碼:
$client = new GuzzleHttpClient();
try {
//正常請求
$res = $client->post('http://api.domain.com/getList.json', [
'multipart' => [
[
'name' => 'image_file',
'contents' => 'abc123'
],
[
'name' => 'size',
'contents' => 'auto'
]
],
'headers' => [
'X-Api-Key' => 'apikey'
]
]);
} catch (GuzzleHttpExceptionClientException $exception) {
//捕獲異常 輸出錯誤
return $this->error($exception->getMessage());
}
總結:使用guzzle時當 請求地址返回狀態(tài)碼不是200時。程序會拋出錯誤。會導致后續(xù)代碼無法運行 所以 需要捕捉錯誤并且反饋出來!