我将如何使用 cron 作业发送 HTML GET 请求? [英] How would I use a cron job to send an HTML GET request?

查看:27
本文介绍了我将如何使用 cron 作业发送 HTML GET 请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想设置一个向 url 发送 http 请求的 cron 作业.我该怎么做?我以前从未设置过 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 教程:http://net.tutsplus.com/tutorials/other/scheduling-tasks-with-cron-jobs/

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 请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆