PHP CURL通过浏览器工作,但不通过Crontab?
问题描述:
当我通过浏览器运行此代码时,它工作得很好,但是当我们通过crontab运行它时,它不起作用。我正在尝试和阅读很多时间,但无法解决此问题。有一些用户遇到类似的问题,我尝试过但是这并没有帮助:this questionPHP CURL通过浏览器工作,但不通过Crontab?
请建议!
感谢
// curl settings and call to reddit
$ch = curl_init('https://www.reddit.com/api/v1/access_token');
curl_setopt($ch, CURLOPT_USERPWD, $clientId . ':' . $clientSecret);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
// curl response from reddit
$response_raw = curl_exec($ch);
$response = json_decode($response_raw);
curl_close($ch);
$accessToken = $response->access_token;
$accessTokenType = $response->token_type;
$username = variable_get('a_reddit_username');
$subredditName = variable_get('sub_reddit_name');
$subredditDisplayName = variable_get('sub_reddit_name');
$subredditPostTitle = $title;
$subredditUrl = $url;
// api call endpoint
$apiCallEndpoint = 'https://oauth.reddit.com/api/submit';
// post data: posting a link to a subreddit
$postData = array(
'url' => $subredditUrl,
'title' => $subredditPostTitle,
'sr' => $subredditName,
'kind' => 'link'
);
// curl settings and call to post to the subreddit
$ch = curl_init($apiCallEndpoint);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_USERAGENT, $subredditDisplayName . ' by /u/' . $username . ' (Phapper 1.0)');
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Authorization: " . $accessTokenType . " " . $accessToken));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
// curl response from our post call
$response_raw = curl_exec($ch);
$response = json_decode($response_raw);
curl_close($ch);
答
使用cron作业像下面。
* 05 * * * wget http://www.YourUrl.com/test.php -O /yourPath/errorLog.txt
为errro LOD“/yourPath/errorLog.txt”代码,而页面加载..
您可以通过在crontab中添加您的电子邮件地址检查什么错误原因。 –
尝试设置假用户代理,如:http://stackoverflow.com/questions/2440729/php-curl-how-can-i-emulate-a-get-request-exactly-like-a-web-browser – MilanG
谢谢但我已经设置了用户代理curl_setopt($ ch,CURLOPT_USERAGENT,$ subredditDisplayName。'by/u /'。$ username。'(Phapper 1.0)');有没有其他的方式来测试它通过什么错误! – jas