curl_setopt отлично работает в браузере, но не во вкладке cron

Команда Cron — wget —spider ‘url’

php file-

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

Решение

Проблема в том, что вы ссылаетесь '$_SERVER["HTTP_USER_AGENT"]' в строке 6, но при выполнении из crontab, $ _SERVER [‘HTTP_USER_AGENT’] не определяется. использование Исеть () проверить, существует ли элемент массива.


<?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

Другие решения

Других решений пока нет …