When I run this code via browser it works very well but when we run it thorugh crontab then it does not work. I am trying and reading from much time but not able to solve this issue. There was similar problem faced by some user I tried but this did not helped : this question
Please suggest !
Thanks
// 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);