$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时当 请求地址返回状态码不是200时。程序会抛出错误。会导致后续代码无法运行 所以 需要捕捉错误并且反馈出来!