我如何使用cron作业发送HTML GET请求?

Joh*_*ohn 2 shell cron http

我想设置一个cron作业,它向网址发送一个http请求.我该怎么做?我以前从未设置过cronjob.

Sea*_*udt 5

cron作业只是一个以常规预设间隔在后台执行的任务.

你几乎可以用任何语言编写作业的实际代码 - 它甚至可以是一个简单的PHP脚本或bash脚本.

PHP示例:

#!/usr/bin/php -q
<?php 

file_put_contents('output.txt', file_get_contents('http://google.com'));
Run Code Online (Sandbox Code Playgroud)

接下来,安排cron作业:

10 * * * * /usr/bin/php /path/to/my/php/file > /dev/null 2>&1  
Run Code Online (Sandbox Code Playgroud)

...上面的脚本将在后台每10分钟运行一次.

这是一个很好的crontab教程:http://net.tutsplus.com/tutorials/other/scheduling-tasks-with-cron-jobs/

您还可以使用cURL执行此操作,具体取决于您要使用的请求方法:

$url = 'http://www.example.com/submit.php';
// The submitted form data, encoded as query-string-style
// name-value pairs
$body = 'monkey=uncle&rhino=aunt';
$c = curl_init ($url);
curl_setopt ($c, CURLOPT_POST, true);
curl_setopt ($c, CURLOPT_POSTFIELDS, $body);
curl_setopt ($c, CURLOPT_RETURNTRANSFER, true);
$page = curl_exec ($c);
curl_close ($c);
Run Code Online (Sandbox Code Playgroud)