Device Detection in the Cloud: DeviceAtlas Personal

In this article we offer full tutorial on how to use the preview release of the service. So what is DA Personal?

The purpose of DeviceAtlas Personal is to make device detection even easier. The main difference between DA Personal and DeviceAtlas, is that Personal is a Web service. It works as follows: a user visits your Web site on his mobile device. You then forward the User-Agent HTTP request header to the DA Personal service, and the response you receive will contain information about the user’s device. That’s it!

Note that this is a preview version of the service, and right now we make no guarantees about stability, performance, or uptime. Note also that device detecton is currently based on the User-Agent string, but we will be adding UAProf support soon too. We invite you to take the service for a spin, and let us know what you think, and if it appeals to you.

Using DA Personal

So DA Personal sounds good. No need to install anything, no need to update device data, so how does it work?

Since DA Personal offers a Web service API, all you have to do to use it is to send an HTTP request and handle the response. This means that you can use any server technology you want with the service. In this tutorial, we use PHP (a Python example is forthcoming too!)

For those eager to get up and running right away, here is some quick code to get you going. (A bit more detail is provided later for those who don’t mind reading). The code below relies on the file_get_contents function, but you can achieve the same with cURL if this function is not available in your setup.

The real work is done in the call to the DA Personal service:

We access the User-Agent header of the requesting device with $_SERVER["HTTP_USER_AGENT"], and we send this header to the DA Personal service specified by DA_URI.

After the code above is executed, the response from the DA personal service comes back in JSON format, and we store it in an array called $da_results. For the purpose of this example, we can output this array to the browser, and it should tell us a few things about the device that we are on. So, the code to output to the browser follows:

”, print_r ($da_results, true));
}
echo “

Time taken: “.(microtime(1) – $start_time).” secs

“;
?>

The results of viewing this page on a mobile device (an iPhone, zoomed, in this case) is shown to the right. The properties returned from the DeviceAtlas database are:

  • displayWidth
  • isBrowser
  • mobileDevice
  • displayHeight

Equipped with this information about the user’s mobile device, your app can now go about adapting content for the device. We include an example toward the end of this article to show how you would resize images to fit the screen width of the requesting device.

That’s it! The code above is all you need to get started with DA Personal. But read on to find out about results caching, and to see an image resizing example.

Caching

It is also possible, and recommended, to implement caching with DA Personal, so that multiple similar requests are not made to the API unnecessarily. In the following code sample we demonstrate how to cache both using cookies and by storing the information sent back by DeviceAtlas as a cached file.

If we set the DA_USE_COOKIES flag, then the code below will store the device data in a cookie identified by Mobi_Mtld_DA_Properties. If, on a subsequent request, that cookie is detected with the request, then we simply read the device data from the cookie, obviating the need for a request to the DA Personal service. So using cookies in this way will give us per-user caching.

If we set the DA_USE_CACHE flag, then caching will be performed using local files. For each User-Agent header that is sent to DA Personal, we store the device data in a local file. If we encounter the same User-Agent header again, our app will now read from the file instead of making the HTTP request to the DA Personal server. It may not always be possible to use file caching, for instance if you do not have file system write access on your server.

The best performance will be experienced when both both cookie and file based caching are turned on. You can tweak this to suit your particular setup.

Image resizing example

As an example of DA Personal in action to get you started, we demonstrate how you might use this service to resize images on your Web site to match the width of the screen of the requesting device, so that you can avoid sending images that are too large to the device. Note that we have left the caching code out of this example for brevity, although it is included in the listing at the bottom of this article.

First we start off as before, by sending the User-Agent header of the device the the DA Personal service.

To resize the image, to the width of the requesting device, we call another script we have written (explained below). We pass into the script the name of the image we want to resize (we use the mobiForge logo), and importantly, we also pass in the displayWidth property returned by DeviceAtlas, stored in $da_results['displayWidth'].

That’s it from the DeviceAtlas end of things, we now go through the image resizing script, which is more of a PHP exercise than DeviceAtlas specific, but we include it here for completeness.

First we retrieve the image name, and the desired image size from the request:

Next we determine some basic information about the image: width, height, and type (JPEG, GIF, or PNG):

We now load the image into memory using the appropriate PHP function dependent on the image type:

In this example, we are going to resize the image to fit the width of the requesting device. First we will check if the image already fits on to the device screen. If so, we leave it as is. Otherwise, we scale the image to fit the display width, by determining the ratio of original width to display width, and then by scaling the height accordingly:

Next, create a new image in memory with the desired dimensions, and we use the imagecopyresampled function to resize the original image and copy into our new image:

Finally, we write the image to output, ensuring that we write the correct Content-type header:

And that’s it! Example images are given below, showing how the page is rendered on various devices. As expected, the image occupies the width of the screen for each device.

  
Image resizing examples: Nokia n95 (left), Nokia n70 (middle), Sharp GX10 (right)

The full script is listed at the end.

Conclusion

In this tutorial, we have demonstrated how to do device detection with DeviceAtlas Personal. We have also demonstrated how to use DeviceAtlas Personal to do some simple image adaptation so that image width matches the width of the requesting device, a common task for mobile Web developers. There are of course many other possible uses for this service – it’s limited only by your imagination! What would you do? Try it out, and let us know how you get on.

We note also that the device detection here is currently based on the User-Agent header alone, although support for use the UAProf URL will also be added soon to help with the detection. We urge you to take the API for a test spin and let us know what you think, and tell us how you are using it. Please leave any feedback in the DeviceAtlas forum.

Image resizing full code listing

Below are the listings for the two PHP files used in the image resizing example. Note that the first, da_personal.php contains the code to cache the results from the DA Personal server. An obvious improvement would be to cache the resized images too, so that CPU is not wasted resizing to the same size over and over.

da_personal.php:

resize_image.php:

Download source code:

11 Comments

  • northernguy69 says:

    I’ve used deviceAtlas and it’s a great tool for testing mobile sites. Another company that is definitely worth mentioning when it comes to device detection and user agent profiles is “Mobile Phone Wizards”. Their product “DetectRight” (www.detectright.com) is a simple web based API that essentially allows you to get a large amount of descriptive details about a device that comes to your site. I use it on mobile sites for the company I work for and I would definitely suggest it to anyone that needs device detection software to take a look at their product.

    Regards

  • Milind says:

    Hi

    I am using DA personal webservice and i want information about vendor & model of mobile device, how can i pull that info from this webservice.

    Any help will be appreciated.

    Thanks in advance.

  • lajeeshk says:

    i use $_SERVER[‘HTTP_USER_AGENT’] for detecting mobile device model .but only some browsers are returns device name and model number like uc web and nokia in built browser.opera mini does not returns the device name and model no..how to resolve this issue

Exclusive tips, how-tos, news and comment

Receive monthly updates on the world of mobile dev.

Other Products

Market leading device intelligence for the web, app and MNO ecosystems
DeviceAtlas - Device Intelligence

Real-time identification of fraudulent and misrepresented traffic
DeviceAssure - Device Verification

A free tool for developers, designers and marketers to test website performance
mobiReady - Evaluate your websites’ mobile readiness

© 2024 DeviceAtlas Limited. All rights reserved.

This is a website of DeviceAtlas Limited, a private company limited by shares, incorporated and registered in the Republic of Ireland with registered number 398040 and registered office at 6th Floor, 2 Grand Canal Square, Dublin 2, Ireland