【发布时间】:2016-08-28 01:45:05
【问题描述】:
我有一个场景。在这里,我使用 Curl 来访问 API。 如果无法访问 API,我必须发送警报电子邮件。那么如何检查是否有请求超时?
这是我的代码。
$alerthttp1 = array();
$string = 'somestring';
$key = md5($string);
$service_url = 'www.someurl.com/'.$key;
$curl = curl_init($service_url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_TIMEOUT, 300);
$curl_response = curl_exec($curl);
$curl_errno = curl_errno($curl);
$curl_error = curl_error($curl);
error_log(json_encode($curl_errno));
error_log(json_encode($curl_error));
if(curl_error($curl) || $curl_response === false || $curl_errno > 0)
{
$info = curl_getinfo($curl);
hlog_errorlog('error occured during curl exec - ' . var_export($info));
hlog_errorlog('error -----------> '. $curl_error);
curl_close($curl);
$alerthttp1[] = array('status' => 'FAILED');//for email alert
}
请建议我,最好的编码方式。
谢谢
【问题讨论】:
-
查看调用的HTTP状态码即可。 php.net/manual/en/function.curl-getinfo.php
标签: php api curl timeout connection-timeout