• About Us
  • Blog
  • Basket
  • Account
  • Sign In
  •  

Blog

Published on Tuesday, April 9, 2013

Automatic PHP Updates With Cron

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.

51Degrees.mobi Detector for PHP ensures that the Premium Data file is kept up to date by offering an update feature that will download and apply any new data released by 51Degrees.mobi.

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:

crontab crontab.txt

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

Comments (0)

Author: Products Team

Categories: Development

Tags: PHP, Cron

Tom Holmes
>

Products Team

Other posts by Products Team
Contact author

Name:
Email:
Subject:
Message:
x

Tags

.NET 2013 2014 4G 51Degrees 5G A.C.Roma A7 ABI Acer Affiliate Marketing Alcatel Amazon AMP Analysis Analytics Android Android 5.0 Lollipop Android Kitkat Android Lollipop Android Media Stick Apache API Apple Apple TV Archos Asha Asian Market ASP.NET Asus Australia Big Data Black Friday Blackberry Blink Browser C C# Case Study CeBIT CES Chrome Cloud CMS combinations Comparison Competition CoolPad COTW Cron CSS3 Data Data Blog Data File Data Model Daydream Denver Design Desire Eye Desktop Detection Device Device Data Device Detection Device Intelligence Device Popularity Device property Device Types Device Use Display DoCoMo Doogee DotNetNuke Download Drupal Email EReader E-Reader Ericsson Evaluation Event Examples EXPLAY Rio Facebook feature Firefox Firefox OS Fly Foundation Framework France Galaxy S3 Galaxy S5 Galaxy Tab A Galaxy Tab A 8.0 Galaxy Tab A 9.7 Germany Global Google Google Daydream GSMA HAProxy Hardware Hisense HTC HTC ONE MAX HTC OS HTML5 HTTP Huawei HUAWEI. UPDATE HUDL Huwaei IBC Icemobile Prime 4.0 IE IFA IIS Image Optimiser Image Optimizer India Infographic Ingeniux Internet usage iOS iOS 7 iOS 8 ipad iPhone iPhone 6 IsEmailBrowser IsWebApp Italy Japan Java Javascript Jolla Kentico Keynote Kindle Kindle Fire Kindle Fire HD Leagoo Lenovo LG Location Log File Analysis LTE Lumia Map Memory Meta Data Mi 4S Micromax Microsoft Miia Style Mobile Mobile Analysis Mobile Analytics Mobile Devices Mobile Marketing Mixer Module Motorola MVC4 MWC MWC 2017 MWC16 MyPhone Native NET New Release News News Letter Nexus Nexus 6 Nexus 9 NFC NGINX Nokia Non-Mobile NVIDIA Omate On7 Opera Opera Mini Operating System Optimisation OS OSX 10.10 OTA Panasonic Patent PC Pebble Performance phablet phone PHP Poland Presentation Press Release Price Band PRIV PS4 Python QMobile QR Codes Redirection Research Reseller Responsive Images RESS Review reviews RIM Ringmark RWD Samsung Scala Screen Screen resolution Screen Size SEO Server Set Box Set Top Box Sharepoint Shark 1 SHIFT phones Sitecore SLUSH Smart TV Smartphone Smartphones Smartwatches Snapdragon Sony Sony Xperia Spain Swedish Beers Symbian Tablet Tablets Tesco Testing Top 5 TOTW TV UDS UK Umbraco Update updates US User Agent UserAgent User-Agent Vendors Version 3 VoLTE VR Wearable Web WebKit WebMatrix White Paper Widgets Widnows WiFi Wiko Wileyfox Windows Windows Phone Xbox XBox One Xiaomi Xperia Xperia z Yosemite Z10 ZenFon 2 ZOPO ZTE