【问题标题】:CRON Job using Curl.使用 Curl 的 CRON 作业。
【发布时间】:2017-07-07 12:09:03
【问题描述】:

我有一个 PHP 代码,它使用另一个 GET 值重新加载自己。像:example.com?number=453 并且它持续这样做了好几天。我在浏览器中这样做。但我发现 cron 工作要好得多。

所以,我需要使用 CURL 重新加载带有新 GET 值(例如 ?number=550)的页面。所以这是我使用的代码(在stackoverflow上找到)

function curl_download($Url){

    // is cURL installed yet?
    if (!function_exists('curl_init')){
        die('Sorry cURL is not installed!');
    }

    // OK cool - then let's create a new cURL resource handle
    $ch = curl_init();

    // Now set some options (most are optional)

    // Set URL to download
    curl_setopt($ch, CURLOPT_URL, $Url);

    // Set a referer
    curl_setopt($ch, CURLOPT_REFERER, "http://www.example.org/yay.htm");

    // User agent
    curl_setopt($ch, CURLOPT_USERAGENT, "MozillaXYZ/1.0");

    // Include header in result? (0 = yes, 1 = no)
    curl_setopt($ch, CURLOPT_HEADER, 0);

    // Should cURL return or print out the data? (true = return, false = print)
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    // Timeout in seconds
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);

    // Download the given URL, and return output
    $output = curl_exec($ch);

    // Close the cURL resource, and free system resources
    curl_close($ch);

    return $output;
}


$output = curl_download("http://www.example.com/yourscript.php?number='$requestsDone'");

在页面的最后,我用这个来调用函数

curl_download($Url);

但我收到此错误:

注意:未定义变量:Url

在最后一行,即curl_download($Url);

这就是我们调用函数的方式对吗?怎么了?还有什么错误或改进,我可以在代码中做吗?

【问题讨论】:

  • $requestsDone 来自哪里?而且它真的不需要用单引号括起来。
  • 它来自代码中的某个地方,兄弟。它不是一个空变量

标签: php curl cron


【解决方案1】:

你没有给变量 $Url 赋值。 在你可以为变量 $Url 赋值之前。就像

$Url = "http://domaintest.com/?number=550";
curl_download($Url);

【讨论】:

  • 它停止了错误。但它不会将我的浏览器重定向到新的 URL。此代码是否重定向浏览器?
  • 或者它只是在后台工作?我怎么知道它是否有效?
  • 你想用这个网址打开浏览器吗?
  • 没有。我只是在我的浏览器中测试代码(而不是 cron 作业)。它在浏览器上什么也不做。
  • 而不是这个,我不能只输入curl_download($output);吗?
猜你喜欢
  • 2013-07-24
  • 1970-01-01
  • 1970-01-01
  • 2017-06-11
  • 2018-12-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多