Detect mobile devices with the WURFL API

By Pierre du Plessis (Pierre)
This component uses the WURFL API to detect any mobile or wireless device, and sets custom paths for your views and css, so you can easily create a mobile version of your website.
This component uses the WURFL API to detect any mobile or wireless device, and sets custom paths for your views and css, so you can easily create a mobile version of your website.

Lets get started.

The WURFL API


First of all, download the WURFL API package here (Make sure to download the 1.1 version).

Extract the entire WURFL folder in the package to your /app/vendors folder.
Create a directory /app/configs/wurfl, and extract everything in the resources folder to that directory.

In the examples/resources directory, extract the file in the wurfl-regression.zip to your /app/configs/wurfl directory.
Edit the wurfl-config.xml file to look like the following:
Download code
<?xml version="1.0" encoding="UTF-8"?>
<wurfl-config>
    <wurfl>
        <main-file>wurfl.xml</main-file>
        <patches>
            <patch>web_browsers_patch.xml</patch>
        </patches>
    </wurfl>
    <persistence>
        <provider>file</provider>
        <params>dir=../../tmp/cache/wurfl</params>
    </persistence>
    <cache>
        <provider>null</provider>
    </cache>
</wurfl-config>

Create the folder /app/tmp/cache/wurfl.

The Component


Create a file /app/controllers/components/mobiledetect.php and put the following in the file:

Component Class:

Download code <?php 
class MobiledetectComponent extends Object
{
    var 
$isMobile false;

    function 
startup(&$controller)
    {
        
$this->controller =& $controller;
    }

    function 
detect()
    {
        
        
App::import'Vendor''WURFL', array( 'file' => 'WURFLManagerProvider.php')); 
        
        
$wurflConfigFile CONFIGS.'wurfl/wurfl-config.xml';

        
$wurflManager WURFL_WURFLManagerProvider::getWURFLManager($wurflConfigFile);
        
        
$requestingDevice $wurflManager->getDeviceForHttpRequest($_SERVER);

        if(
$requestingDevice->getCapability('is_wireless_device') == 'true')
        {
                        
$this->isMobile true;
            
$this->setMobile();
                        
$this->controller->theme 'mobile';
                  
$this->controller->layoutPath 'mobile';
        }
        
       
    }
}
?>

In your app_controller, remember to include the component like this:
Download code
var $components = array('Mobiledetect');

and add to the beforeFilter function the following:
Download code
function beforeFilter()
{
    $this->Mobiledetect->startup($this);
    $this->Mobiledetect->detect();
}

And thats it! Now you can create a custom layout file for your mobile website, located under /app/views/layouts/mobile.
You also need to put your css in a directory in /app/webroot/themed/mobile/css
and (optional)images in /app/webroot/themed/mobile/css/img.
Your views need to go to /app/views/themed/mobile.

Your done! Now you can create a mobile version of your website.

 

Comments 1350

CakePHP Team Comments Author Comments
 

Comment

1 Using android phone to test the function ! - Error !

Fatal error: Uncaught exception 'WURFL_WURFLException' with message'Device with device id generic_android is not defined.' in D:\Apache\mysoft\www\vendors\WURFL\DeviceRepository.php:51 Stack trace: #0 D:\Apache\mysoft\www\vendors\WURFL\WURFLService.php(95): WURFL_DeviceRepository->getDevice('generic_android') #1 D:\Apache\mysoft\www\vendors\WURFL\WURFLService.php(47): WURFL_WURFLService->getWrappedDevice('generic_android') #2 D:\Apache\mysoft\www\vendors\WURFL\WURFLManager.php(42): WURFL_WURFLService- >getDeviceForRequest(Object(WURFL_Request_GenericRequest)) #3 D:\Apache\mysoft\www\vendors\WURFL\WURFLManager.php(57): WURFL_WURFLManager- >getDeviceForRequest(Object(WURFL_Request_GenericRequest)) #4 D:\Apache\mysoft\www\controllers\components\mobiledetect.php(20): WURFL_WURFLManager->getDeviceForHttpRequest(Array) #5 D:\Apache\mysoft\www\controllers\links_controller.php(13): MobiledetectComponent->detect() #6 D:\Apache\cakecore\cake\dispatcher.php(209): BacklinksController->befo in D:\Apache\mysoft\www\vendors\WURFL\DeviceRepository.php on line 51
Posted Mar 14, 2010 by hugo phang
 

Comment

2 Scratching the surface

Nice basic wurfl setup for Cake. But that is a lot of code for simply detecting a mobile device. Wurfl has so much more to offer and I would have liked to see it put to more use.

People tickled by mobile detection should do this:

debug($requestingDevice->getAllCapabilities());

That will print almost 500 pieces of information about the device. (logging it is probably better)

To get the benefits worth the "cost" of a heavy data-set like the wurfl database (current version is 14,5MB) you probably want to optimize things like images, video conversions, applications types or any other "content" presented to the mobile browser.

When fully leveraged, Wurfl is really powerful.

Btw: The "real" database is located and updated at: http://wurfl.sourceforge.net/ The one mentioned in the article is a limited test database. Remember to empty the cache after updating the wurfl xml.
Posted Mar 15, 2010 by Martin Westin
 

Comment

3 Package?

I'm using wordpress on my website dog tags for dogs, do I really need to donwload the package or does wordpress have a widget for that.
Posted Apr 14, 2010 by Jayme
 

Comment

4 Cake

Very nice tutorial on setting up wurfl with cake. Will be taking advantage of this very soon in one of my apps.
-----------------
Aaron of sell gold
Posted Apr 17, 2010 by Aaron Langston
 

Comment

5 nice set up

I have downloaded the WURFL API as per your instructions. Its really a nice basic WURFL set up for Cake. Besides just detecting any mobile or wireless device WURFL has much more to offer . I will like to see more about its usage here from you. Even then I am taking its advantage for one of my applications. To get the maximum benefit from WURFL database, I optimized the things like images, video conversions, predictive dialer and other contents presented to mobile browser.
Posted Apr 19, 2010 by brown
 

Comment

6 Good

Thank you for being so informative, I am totally impressed, just keep us updated!
Freestyle Medela
Posted Apr 21, 2010 by nicole hogan
 

Comment

7 Capabilities are Available to the Server

The process is finished and the capabilities are now available for use in your scripts. One common use, for example, is to redirect mobile devices to a mobile version of the site:
Removalists4u
require_once('./TeraWurfl.php');
$wurflObj = new TeraWurfl();
$wurflObj->getDeviceCapabilitiesFromAgent();

// see if this client is on a wireless device
if($wurflObj->getDeviceCapability("is_wireless_device")){
header("Location: http://www.removalists4u.com.au");
}
?>
Posted Apr 26, 2010 by Calvin
 

Comment

8 WURFL API

The WURFL is an XML configuration file which contains information about capabilities and features of many mobile devices. WURFL Web Service provides a RESTful interface to the WURFL database. If a user is writing a web application for mobile phones and needs to know the capabilities of a specific device, they can use this service to query the exact capabilities of the device without having to build an implementation of WURFL. Responses are returned in JSON format.
Posted Apr 27, 2010 by Jennifar
 

Question

9 WURFL PHP API

I have a simple stupid question. Here's the problem, for years, my company has been using the old WURFL PHP API. Now, with the release of the new WURFL PHP API that promises a better performance, we want to use it.

What I want to ask is, how do I update the cache? Can I use the old *update_cache.php*?
Electricians4u
Posted May 1, 2010 by Eric
 

Comment

10 Thanks for.........


Thanks for generously sharing so many useful tips.
I am about to begin a blog and this will help me tremendously.
Of course it has given me many things to consider as I update my website too.
I appreciate it.
Calvin00
Posted May 3, 2010 by Calvin
 

Comment

11 A couple of issues here

Can the author respond to the first comment? It doesn't detect the Android phone - I would think that would a showstopper for most people, including myself.

It doesn't seem like anybody has actually tested this code, because the component code that is posted doesn't work out of the box. First the call $this->setMobile() is an illegal call because that function doesn't even exist. Second you need to set the view variable of the controller in order to use the new mobile theme. So in the if clause in the detect() function insert this:

 $this->controller->view = 'Theme';

That should get you up and running. Now on to the vendor code. The latest release 1.1/1.r2 is broken - I don't know why it's posted. It died on a missing argument error. The 1.1/1.1r release works, but is about a year old now - is that why it doesn't know about android?? I noticed the wurfl.xml file in 1.r1 is much smaller than the wurfl.xml file in 1.r2. Can I take the one from 1.r2 and drop it in the 1.r1 file structure?

Does anybody know of a better wireless detection php vendor?


Andy
Posted May 3, 2010 by Andy
 

Comment

12 Update WURFL

Android is a pretty new device family. To get support for it and other new devices just update the WURFL database regularly.
Posted May 12, 2010 by Martin Westin
 

Comment

13 A First Look at the New WURFL API for PHP

About a month ago, the New WURFL API for PHP was officially released. While the code had been available in one form or another for some time, the official release coincided nicely with the early stages of a new project at work, so it seemed like an appropriate time to have a look at the APIOak Tree and see if it was something we wanted to use.

By way of a refresher, WURFL is a "Device Description Repository" - a huge open-source XML-based database of information regarding mobile handsets and their capabilities. I've discussed WURFL in the past, for example here. Prior to this release, the only practical method of querying WURFL in real time from PHP was via a library named Tera-WURFL, which I blogged about here. In fact, both WURFL and Tera-WURFL were covered in an article I wrote for php|architect magazine last year.
Cleaners4u
Posted May 12, 2010 by cleanersforu
 

Comment

14 Thank you

The API uses log4net logging library, so please check there for configuration. Mind you, there is a lot of debugging going on. A sample is included in the sample web application.
Dog Training Tips
Posted Jul 13, 2010 by freshegg
 

Comment

15 thanks

Don’t Usually Reply to Posts But I Will in this Case! Of Course, What a Great Site and Informative Post, I Always Wanted to Write in My Site Something Like That. Thank You! …mezzi case reviews
Posted Jul 14, 2010 by Jim
 

Comment

16 Give it a try

I am going to give it a try for my website and make a post about the WURFL API in german, if it works out well. Thanks.
Posted Jul 14, 2010 by Pero Dordic
 

Comment

17 Thank you

WURFL is a collection of libraries and command line tools for using and manipulating the WURFL.
Sheesham Furniture
Posted Jul 15, 2010 by Glasstile
 

Comment

18 Thanks

I've discussed WURFL in the past, for example here. Prior to this release, the only practical method of querying WURFL in real time from PHP was via a library named Tera-WURFL, which I blogged about here.

Designer Resources, Php Tutorial
Posted Jul 15, 2010 by allby sie
 

Comment

19 Great Tips!

Thanks for great guidelines about creating the folder /app/tmp/cache/wurfl. It works great!

Research paper writing | custom research paper
Posted Jul 16, 2010 by Jim Diggerson
 

Comment

20 Good application

WURFL API service will prove helpful n finding the stolen mobile devices. This is really nice.
micromax bling
Posted Jul 18, 2010 by Mike Stehen
 

Comment

21 Thank you

I have a simple stupid question. Here's the problem, for years, my company has been using the old WURFL PHP API. Now, with the release of the new WURFL PHP API that promises a better performance, we want to use it.
Royal Navy Careers
Posted Jul 20, 2010 by James smilth
 

Comment

22 thank you

Thank you nice post :)
images upload- Resim yukleme Images upload and resim yukleme sitesi.
Posted Jul 20, 2010 by mark
 

Comment

23 Thank you

Posted Jul 21, 2010 by mark
 

Comment

24 abe

When the majority of people will see the realistic way to solve that over time that it starts to blend suddenly. auto transport Yes, the will be read again without any doubt.
Posted Jul 22, 2010 by dan kaylee
 

Comment

25 Web Conferencing

Scientific studies have shown that the ecological value of golf courses can be very high. The guidance provided opportunities for the conservation of biodiversity and cultural landscape but are not always used in full. Subject of the environmental concept involves the idea of the environment to continuously improve on web conferencing courses and thereby achieve measurable progress in environmental protection.
Posted Jul 26, 2010 by Sylfaen
 

Comment

26 pool filters

I was about to purchase something like IU, and got hacked when I ran it for a test. Hackers are getting good these days, aren't they?
pool filters
Posted Jul 29, 2010 by sanejenny
 

Comment

27 DUI Lawyer

This is nice post which I was awaiting for such an artice and I
have gained some useful information from this site.I admire the
valuable information you offer in your articles. I will bookmark
your blog and have my friends check up here often.
DUI Lawyer
Posted Jul 30, 2010 by pramod kr
 

Comment

28 Fantastic!

I've discussed WURFL in the past, for example here. Prior to this release, the only practical method of querying WURFL in real time from PHP was via a library named Tera-WURFL, which I blogged about here.
Pepper Spray
Posted Jul 31, 2010 by Joseph Morris
 

Comment

29 gxdsgdfgfdgf

I've discussed WURFL in the past, for example here. Prior to this release, the only practical method of querying WURFL in real time from PHP was via a library named Tera-WURFL, which I blogged about here.
Pepper Spray
Posted Jul 31, 2010 by aaron maxwell
   

Comment

31 hello

This is really helpful.Hydraulic jacks Setting goals is something which I often struggle with, as I tend to make them either unachievable or I don’t make any at all. Hydraulic jacks It’s easy to think about what you want to achieve, but unless you write it down and make some solid goals with time periods,Hydraulic jack it is unlikely that they will ever be achieved. Thanks a lot.
Posted Jul 31, 2010 by jini