curl_setopt在浏览器中工作正常但在cron选项卡中没有

时间:2014-11-16 05:37:35

标签: php curl crontab

Cron命令 - wget --spider' url'

php文件 -

function getHTML($url,$timeout)
{

$ch = curl_init($url); // initialize curl with given url

       curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"]); //set  useragent

       curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // write the response to a variable

       curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // follow redirects if any

       curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); // max. seconds to execute

       curl_setopt($ch, CURLOPT_FAILONERROR, 1); // stop when it encounters an error

       return @curl_exec($ch);

}

无错误 远程文件存在并且可能包含更多链接,但是递归被禁用 - 而不是检索。

没有得到我错的地方。

1 个答案:

答案 0 :(得分:0)

问题是你在第6行引用了'$_SERVER["HTTP_USER_AGENT"]',但是当从crontab执行时,没有定义$ _SERVER ['HTTP_USER_AGENT']。使用isset()检查数组项是否存在。


<?php
function getHTML($url,$timeout)
{

       $ch = curl_init($url); // initialize curl with given url

       curl_setopt($ch, CURLOPT_USERAGENT, (isset($_SERVER["HTTP_USER_AGENT"]) ? $_SERVER["HTTP_USER_AGENT"] : "PHP/5.5")); //set  useragent

       curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // write the response to a variable

       curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // follow redirects if any

       curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); // max. seconds to execute

       curl_setopt($ch, CURLOPT_FAILONERROR, 1); // stop when it encounters an error

       return @curl_exec($ch);

}
相关问题