WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDES  |  PHP CLASSES  |  CODE SEARCH  |  ARTICLES SEARCH  |  PHP FORUMS  |  PHP MANUAL  |  PHP FUNCTIONS LIST  |  WEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Submit Your Code
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
WeberDev's Monthly code contest PHP Code Contest
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Index
Web Development Resources
Web Development Content
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
Mobile Dev World

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : Calling PHP scripts as CRON jobs without having CGI PHP available
Categories : CURL, Cron Update Picture
Simon Booth
Date : Aug 06th 2002
Grade : 3 of 5 (graded 9 times)
Viewed : 35617
File : No file for this code example.
Images : No Images for this code example.
Search : More code by Simon Booth
Action : Grade This Code Example
Tools : My Examples List

  Submit your own code examples 
 

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.



Newbie Notes #8 - A cron trick
Categories : PHP, CURL, Beginner Guides
PHP Youtube Downloader - This is a set of PHP functions that can be used to download movies from Youtube.com.
Categories : PHP, CURL, Regexps
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
Returns Yahoo! Address Book and Messenger List as an Array
Categories : PHP, PHP Classes, CURL
Using cURL to download a file programmatically.
Categories : PHP, PHP Extensions, 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
Easily Grant Temporary SSH Access to yourself when in remote location
Categories : PHP, Linux, Cron, Security
Open remote https url using Curl and accept cookie
Categories : PHP, CURL
Amazon.com API, CURL-REST Parser. Obtain data about Amazon products (PHP5 +)
Categories : PHP, Ecommerce, XML, Web Services, CURL
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
Using data from a string.
Categories : PHP, Strings, CURL
Url To Pdf Report By Remote Application
Categories : PHP, PHP Classes, PDF, CURL