Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How would I use a cron job to send an HTML GET request?

Tags:

http

shell

cron

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.

like image 262
John Avatar asked Oct 15 '25 04:10

John


1 Answers

A cron job is just a task that get's executed in the background at regular pre-set intervals.

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 Example:

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

file_put_contents('output.txt', file_get_contents('http://google.com'));

Next, schedule the cron job:

10 * * * * /usr/bin/php /path/to/my/php/file > /dev/null 2>&1  

... the above script will run every 10 minutes in the background.

Here's a good crontab tutorial: http://net.tutsplus.com/tutorials/other/scheduling-tasks-with-cron-jobs/

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);
like image 199
ghstcode Avatar answered Oct 16 '25 20:10

ghstcode



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!