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

前端 未结 4 645
没有蜡笔的小新
没有蜡笔的小新 2021-01-22 14:28

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.

4条回答
  •  自闭症患者
    2021-01-22 15:00

    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
    

    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);
    

提交回复
热议问题