|
|
|
It's very often desirable to have things run in the small hours of the morning. It's handy for many of us to write most of the things we need in PHP. These two goals are often mutually incompatible as the vast majority of web servers have only the module version of PHP installed (which you can't call from the command line).
There is a simple way around all this that proves extremely useful for all sorts of situations. The most common uses that I've personally had for this method is to generate overnight emails and to do some mass database operations. There are, however, many additional uses for the procedure described here.
First off get yourself a copy of CURL from http://curl.haxx.se and install it - note that you may already have CURL, possibly not installed though, as it is included with many Linux distributions.
CURL is, co-incidentally, available for use by your own PHP scripts via a set of functions - we're not going to do that here though.
The command-line curl client allows you to fetch a page from a webserver (or one of many other Internet sources) and by default dumps the resultant output to the screen. If you try typing something like...
curl http://localhost/
at the command prompt you'll very likely be rewarded with the some raw HTML as you just asked the program to fetch the default page on the local webserver.
Now, let's suppose you've got a script that, for example, does a mailshot to a load of users that you can run manually from a browser. If the url for this page was http://www.somewhere.com/nightly/mailing.php then using curl on the URL would fetch
that page.
As long as this hypothetical mailing script requires no input from the user - assuming it's written in such a way that it just sends out a load of emails and that's as far as it goes in regard to user interaction you've got a script that can be run overnight from a CRON job using CURL.
Of course, if you haven't got such a script then you write one or modify a script you currently use so it is such a script.
Now, naturally you don't want a random web user coming along and triggering your script manually. As this is the case you'll have it in some password protected directory (look elsewhere for information as to how to do this if you don't know). Thankfully CURL lets us supply HTTP authentication on the command line, so as long as your script is proteced by a .htaccess file as opposed to some snazzy logon script in PHP you've got everything you need to send out those nightly emails without someone accidentally stumbling accross them and sending them manually for you.
You need to know the path for the curl command to use it from a CRON job (coming shortly), which curl should give you that information (probably /usr/bin/curl)
The command line to send out the example mailing script becomes...
| | /usr/bin/curl -u username:password http://www.somewhere.com/nightly/mailing.php | |
Finally, we just need to insert that little lot into the crontab. The command crontab -e is probably your best bet in order to make the entry if you're not used to this - and have a read of the man page before you give it a go...
something along the lines of ...
| | 05 1 * * * /usr/bin/curl -u username:password http://www.somewhere.com/nightly/mailing.php | |
should cause the script to be run at 1:05 AM, again check your crontab documentation
as this bit can vary.
On the whole, I think you'll agree, a very useful facility that can solve many a
problem easily.
|
|
| These PHP Classes Check if a host is alive using various methods. Categories : PHP, PHP Classes, Sockets, CURL | | | Import the yahoo address book. Categories : PHP, CURL, Authentication | | | curl_close -- Close a CURL session Categories : PHP, PHP Functions, CURL | | | Newbie Notes #8 - A cron trick Categories : PHP, CURL, Beginner Guides | | | Open and Close your website in fixed times . Categories : PHP, PHP Classes, Cron, Date Time | | | A function to check if a URL exists Categories : PHP, CURL, HTTP | | | Easily Grant Temporary SSH Access to yourself when in remote location Categories : PHP, Linux, Cron, Security | | | Yahoo! Messenger Friend List Categories : PHP, CURL, HTML and PHP | | | Amazon.com API, CURL-REST Parser. Obtain data about Amazon products (PHP5 +) Categories : PHP, Ecommerce, XML, Web Services, CURL | | | Using cURL to download a file programmatically. Categories : PHP, PHP Extensions, CURL | | | PHP Youtube Downloader - This is a set of PHP functions that can be used to download movies from Youtube.com.
Categories : PHP, CURL, Regexps | | | Returns Yahoo! Address Book and Messenger List as an Array Categories : PHP, PHP Classes, CURL | | | Open remote https url using Curl and accept cookie Categories : PHP, CURL | | | Link Manager for Link Exchangers Categories : PHP, PHP Classes, Databases, MySQL, CURL | | | Sample AIM (Advanced Integration Method) PHP Script For Authorize.net using CURL Categories : CURL, Ecommerce, PHP | |
|
|
|