PHP CURL是通过浏览器而不是通过Crontab工作的?

时间:2017-04-24 12:01:50

标签: php curl crontab

当我通过浏览器运行此代码时,它运行良好,但是当我们运行它时,它不起作用。我正在尝试和阅读很多时间,但无法解决这个问题。我试过的一些用户遇到了类似的问题,但这没有帮助:this question

请建议!

谢谢

    // 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);

1 个答案:

答案 0 :(得分:0)

使用如下的cron作业。

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

" /yourPath/errorLog.txt"页面加载时errro lod的代码..