2014-11-16 157 views
0

cron命令 - wget的--spider 'URL'curl_setopt工作正常,在瀏覽器,但不是在cron標籤

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); 

} 

無差錯 遠程文件是否存在,並可能包含進一步的聯繫,但遞歸被禁用 - 不檢索。

沒有得到我錯誤的地方。

回答

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); 

} 
+0

同樣的錯誤 - 遠程文件存在,並可能包含進一步的聯繫,但遞歸是禁用 - 不取回。 – asjain

+0

我正在使用godaddy服務器。他們有可能否認通過cron作業從url獲取數據? – asjain

+0

現在它的工作,我將http_user_agent更改爲isset($ _ SERVER [「HTTP_USER_AGENT」])? $ _SERVER [「HTTP_USER_AGENT」]:「PHP/5.5」,我將我的cron命令更改爲/ usr/local/bin/php -q'filepath'。非常感謝.. – asjain

相關問題