php - 调用 URLFetch 失败,出现 url x 的应用程序错误 5

标签 php google-app-engine curl cron urlfetch

背景:

我正在使用谷歌应用引擎,但我的网站抓取工具中有一个奇怪的错误。

我有一个后端,每晚都会自动抓取网站。这是由于 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/

相关文章:

php - 数据库 HTML 中的 TCPDF 图像不显示

php - 如何检查查询是否没有返回结果并使用不同的查询?

google-app-engine - 如何以编程方式访问安全且经过身份验证的 Appengine 应用程序

python - App Engine 批量加载器

java - YML 文件中的 HikariCP DataSourceProperties

PHP CURL 重定向

php - 递归获取 parent 姓名

php - 调用未定义的方法 UserModel::addUser()

curl - PHPUnit 无法识别 cURL 扩展 - 无法安装 selenium 测试

php - 使用 PHP 运行 Shell 脚本