安裝guzzle
composer require guzzlehttp/guzzle使用代碼:
$client = new GuzzleHttpClient();
try {
//正常請(qǐng)求
$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) {
//捕獲異常 輸出錯(cuò)誤
return $this->error($exception->getMessage());
}
總結(jié):使用guzzle時(shí)當(dāng) 請(qǐng)求地址返回狀態(tài)碼不是200時(shí)。程序會(huì)拋出錯(cuò)誤。會(huì)導(dǎo)致后續(xù)代碼無(wú)法運(yùn)行 所以 需要捕捉錯(cuò)誤并且反饋出來(lái)!