当由于网络错误或远程服务器使用curl或get_file_contents而导致连接超时时,由于某种原因它没有响应,它会终止我的脚本。
我正在循环中进行这些远程调用,如果失败则会导致我的脚本死亡。
如果某个特定帖子失败,它会在循环中转到下一个而不是死亡的脚本,那么最好的方法是什么?
答案 0 :(得分:1)
首先为 CURL 设置一个超时限制参数:
curl_setopt($ch, CURLOPT_TIMEOUT, 1800);
curl_exec()
来电的结果会显示请求是否成功:
for(/* anything */) {
$ch = curl_init();
//...
$result = curl_exec($ch);
if (!$result) {
continue; // use this to jump to the next loop
}
// this code will not be executed if the request failed
}
答案 1 :(得分:0)
使用try和catch块:
foreach (remote call....) {
try {
your current code
} catch (Exception $e) {
what to do if it fails
}
}
如果您的整个脚本需要更多时间来运行,请使用:
set_time_limit ( 500 ) // or however many seconds you need