0

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);
Community
  • 1
  • 1
jas
  • 177
  • 1
  • 10
  • You check what error cause by adding your email address in crontab. – Ahmed Ginani Apr 24 '17 at 12:05
  • Try setting fake user agent, like: http://stackoverflow.com/questions/2440729/php-curl-how-can-i-emulate-a-get-request-exactly-like-a-web-browser – MilanG Apr 24 '17 at 12:10
  • Thanks but I have set user agent like curl_setopt($ch, CURLOPT_USERAGENT, $subredditDisplayName . ' by /u/' . $username . ' (Phapper 1.0)'); Is there any other way to test what error it thorugh !! – jas Apr 24 '17 at 12:15
  • get sure the curl php-extention is included in PHP-CLI's php.ini, otherwise use `dl()`. – Deadooshka Apr 24 '17 at 13:10
  • @Deadooshka when I check phpinfo() it shows curl extension is enabled, please suggest if there is some other way to test! – jas Apr 24 '17 at 13:13
  • Clarify the context the script runs in. The crontab command line is not given. – Deadooshka Apr 24 '17 at 13:29
  • It uses docker + nigex scheduler https://golang.org/pkg/net/http/ command is set via 0 */5 * * * * job_cronurl_red8yuhgi similar cmmands works for other things but one including curl not working. https://godoc.org/github.com/robfig/cron#hdr-CRON_Expression_Format – jas Apr 24 '17 at 13:32
  • you may get a debug info eg with this `file_put_contents('/path/to/dump.txt', print_r(get_loaded_extensions(), true));`. Also put there the `$_SERVER` or a constant with needed info. – Deadooshka Apr 24 '17 at 14:05

1 Answers1

0

use cron job like below.

* 05 * * *  wget http://www.YourUrl.com/test.php -O /yourPath/errorLog.txt

"/yourPath/errorLog.txt" code for errro lod while page is loading..

kaustubh
  • 178
  • 7