【问题标题】:PHP CURL working via browser but not via Crontab?PHP CURL 通过浏览器工作,但不能通过 Crontab?
【发布时间】:2017-04-24 20:26:32
【问题描述】:

当我通过浏览器运行此代码时,它运行良好,但是当我们通过 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);

【问题讨论】:

  • 您可以通过在 crontab 中添加您的电子邮件地址来检查导致错误的原因。
  • 尝试设置假用户代理,例如:stackoverflow.com/questions/2440729/…
  • 谢谢,但我已经设置了用户代理,例如 curl_setopt($ch, CURLOPT_USERAGENT, $subredditDisplayName . ' by /u/' . $username . ' (Phapper 1.0)');有没有其他方法可以测试它到底是什么错误!!
  • 确保 curl php-extention 包含在 PHP-CLI 的 php.ini 中,否则使用 dl()
  • @Deadooshka 当我检查 phpinfo() 时显示 curl 扩展已启用,请建议是否有其他测试方法!

标签: php curl crontab


【解决方案1】:

使用如下所示的 cron 作业。

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

"/yourPath/errorLog.txt" 页面加载时出错的代码..

【讨论】:

    猜你喜欢
    • 2018-05-10
    • 1970-01-01
    • 2015-08-11
    • 1970-01-01
    • 2017-05-07
    • 2011-07-15
    • 2016-11-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多