问题描述
我想设置一个cron作业,发送一个http请求到一个url。我该怎么做?我从来没有设置过cronjob。
I would like to set up a cron job which sends an http request to a url. How would I do this? I have never set up a cronjob before.
推荐答案
cron作业只是一个任务,设置间隔。
A cron job is just a task that get's executed in the background at regular pre-set intervals.
你几乎可以用任何语言写作业的实际代码 - 它甚至可以是一个简单的php脚本或bash脚本。
You can pretty much write the actual code for the job in any language - it could even be a simple php script or a bash script.
PHP示例:
#!/usr/bin/php -q
<?php
file_put_contents('output.txt', file_get_contents('http://google.com'));
接下来,计划cron作业:
Next, schedule the cron job:
10 * * * * /usr/bin/php /path/to/my/php/file > /dev/null 2>&1
...上述脚本将每10分钟运行一次
... the above script will run every 10 minutes in the background.
这里是一个很好的crontab教程:
Here's a good crontab tutorial: http://net.tutsplus.com/tutorials/other/scheduling-tasks-with-cron-jobs/
您也可以使用cURL根据您要使用的请求方法执行此操作:
You can also use cURL to do this depending on what request method you want to use:
$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);
这篇关于我如何使用cron作业发送HTML GET请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!