背景:
我正在使用谷歌应用引擎,但我的网站抓取工具中有一个奇怪的错误。
我有一个后端,每晚都会自动抓取网站。这是由于 php 中的时间限制而被推送到 pushQueue 的任务引发的。
问题:
当我手动运行创建任务的脚本时,任务按预期完成且没有错误。但是,当 cron 启动任务时,出现以下错误。
Call to URLFetch failed with application error 5 for url x
代码:
function url_get_contents ($Url) {
global $retry;
try {
if (!function_exists('curl_init')){
die('CURL is not installed!');
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $Url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
curl_close($ch);
return $output;
} catch (Exception $e) {
syslog(LOG_INFO, 'Caught exception: ', $e->getMessage());
if($retry > 0){
$retry -= 1;
return url_get_contents($Url);
}
else{
return null;
}
}
}
感谢 syslog,我可以看到 $url 很好,这让我发疯,因为它在手动启动完全相同的脚本而不是通过 cron 时工作。
我该如何解决这个问题?
提前致谢。
最佳答案
“申请错误5”表示超过了申请截止时间。
您可以使用选项 CURLOPT_TIMEOUT 增加请求的截止日期,因此您的代码可能类似于:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $Url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 60); // 60 second timeout
$output = curl_exec($ch);
关于php - 调用 URLFetch 失败,出现 url x 的应用程序错误 5,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31045622/