This guide shows how 51Degrees.mobi Premium Device Data can be automatically updated in a PHP environment. If you are a Lite Data user you still need to manually update your data file, or upgrade to Premium Data to keep your Device Data up to date automatically.
Unfortunately, a vanilla PHP install will only respond to web requests, so cannot be made to update by itself regularly. Instead, updates can be triggered by requesting '51DUpdate.php'. Visiting this page will cause the PHP runtime to check for a newer version of 51Degrees Device Data and install it. Before continuing any further with this guide, check that you are already able to get data updates in our user guide.
To automate this we can use a cron job, although any process that can request the webpage will have the same effect.
A cron job is a basic program used for scheduling tasks and is available in just about any Unix or Unix-style system. To work, it first needs a crontab; a file that tells the scheduler what task needs doing and how often.
Cron tabs work by specifying the time with parts seperated by a space, and then the task:
'minute hour day month day-of-week command-line-to-execute'
The asterisk indicates that the cron expression will match for all values of the field; e.g., using an asterisk in the 4th field (month) would indicate every month.
For example, our cron tab looks like this:
* * * * 4 /usr/bin/wget http://YourWebsite.com/51Degrees/51DUpdate.php
Note that several *'s are used to specify that the job should happen only every thursday, as our Premium Device Data is released on wednesday every week. Also note that a full path is used for wget. Some servers implement tighter security so a cron job needs to be explicitly told exactly what command to run. Wget is used even though it isn't a browser, as it will create a web request which is sufficient to trigger the update.
This line should be saved as an ASCII text file with a .txt extension. In this example it'll be called 'crontab.txt'. You should also use a simple text editor, as something like Microsoft Word may add extra artifacts to the file that will not work with cron.
The cronjob now needs to be added to the schedule. To do this, use an FTP client(or some other method) to copy the cronjob over to your server.
Once the file has been copied across, you need to open a terminal window and use the following command to add the file to the scheduler:
No output signifies a success and if an error was present, check that the crontab file is properly formatted.
51DUpdate.php will be polled according to your schedule. You can check when your data was published by using the following code:
<?php require_once('51Degrees.mobi.metadata.php'); echo $_51d_meta_data['Date']; ?>
Note that this provides the date that the data was published, not the date it was downloaded. The data is published weekly, but if you find that you're not receiving updates see the update section of the user guide.
For more information about the crontab process, you can visit LinuxManPages.com