0
當我通過瀏覽器運行此代碼時,它工作得很好,但是當我們通過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);
您可以通過在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