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


Published on Friday, August 19, 2011

Create Your Own .NET Mobile Detection Cloud Service to use with PHP

51Degrees.mobi Foundation is one of the most used mobile device detection libraries for .NET available. But what if you’re not using .NET? Thanks to the magic of web services you can have all the advantages of an expensive cloud service for the free and use it with PHP, Java, Ruby, Python and anything else that can consume web services. In this article we’ll be looking at doing this from PHP. This guide assumes you are able to host both a ASP.NET and a PHP site."But I've never worked with .NET"No problem - as is our way at 51Degrees.mobi we've made it as simple as can be: 

Step 1 - Understanding Your Cloud

We advise hosting your own cloud service rather than using a service from us because of the inherent risks in relying on someone else's servers. Using an external service means having to send data to another far away server, waiting for it to be processed and then waiting for a reply. This adds a huge overhead to your website's performance and it’ll never be as fast a local solution, as foundation is in ASP.NET. There is also added risk that their Cloud Server won't be available. Therefore we strongly recommend having your PHP site and .NET as close together. However, for the purposes of showing the process in this guide and for debugging you can use our demo detector service (demos.51degrees.mobi/detector/mobiledevice.asmx). 

To create the cloud we'll be using our open source Foundation Detector sample to create the web service. You can see an online demo of it here. This site is an example of how to configure the foundation and to show detection and redirection in action. It also has a .asmx file defining a SOAP web service that can be used to interrogate any capability and useragent, not just the useragent of the browser viewing the site. Consuming a service is just as easy as initialising a class.If you're unfamiliar with Visual Studio and .NET that doesn't matter. The code is already functional and configured, all you need to do is publish the detector site and host it.Note – PHP and ASP.NET are often hosted on different server software, ie Apache and IIS. However, PHP can be used with either server. For my tests I used IIS to do the hosting. See this link - http://php.iis.net/. Microsoft WebMatrix can also be used which provides a nice IDE and sees to all the behind-the-scenes configuration.

Step 2- Testing your Cloud

You can test if the Detector is running easily enough by visiting it in your browser. You should see if checkUA.aspx is working. This page uses the webservice that will be used in PHP later to find information about useragents. You can also visit MobileDevice.asmx itself.This pages lists the methods available to use in the web service. If you can see this you're ready to start using it with PHP.

Step 3 - Accessing your Cloud

Now that your service is running all you need to do is include these functions in your PHP page:

//Retrieves the SoapClient and saves the WSDL file locally for performance
function GetSoapClient()
    //Constants to find and save WSDL file. You should change the Url to your own service. Don't forget '?WSDL', that is very important
    $filePath = "51DService.xml";
    //Obtain WDSL file
            //File doesn't already exist
            //Open the url and save xml contents to a string
            $raw = file_get_contents($url);
            //Save it to the filepath
            file_put_contents ($filePath, $raw);
        //File now definitely exists, create a soap client with it.
        //Uses the browser user agent but this could be set to anything.
        return @new SoapClient($filePath, array('user_agent' => $_SERVER));
    catch(Exception $e)
        //Something went wrong
        return $e->GetMessage();
//Returns a single capability as a string
function GetCapability($capability)
        //Obtains a SoapClients
        $client = GetSoapClient();
        //Pass the service the parameter you want
        $params = array ('capability' => $capability);
        //Get the capability and then return it
        $result = $client->GetCapability($params);
        return $result->GetCapabilityResult;
    catch(Exception $e)
        //Something went wrong
        return $e->GetMessage();
//Takes a string array of capabilities and returns them in that order.
//If a capability coudn't be found the service returns null for that element.
function GetCapabilities($capabilities)
        //Open SoapClient
        $client = GetSoapClient();
        //Pass parameters
        $params = array ('capabilities' => $capabilities);
        $result = $client->GetCapabilities($params);
        //A multidimensional array object is returned.
        //Cast as array.
        $var = (array)$result->GetCapabilitiesResult;
        //Take the 'string' element.
        return $var;
    catch(Exception $e)
        //Something went wrong.
        return $e->GetMessage();

These functions create the soap client and stores the WDSL file for later use. They return the given capability (or capabilities) for the browser viewing the sight, although any useragent can be interrogated in the SoapClient constructor. Allowed capabilities are any of the wurfl or .NET names. To use the functions you'll need something like this -  

  <title>Web Service Test</title>
//Code above
echo 'Useragent of device: ';
echo '<p/>';

echo 'Brand name of device: ';
echo GetCapability('brand_name');
echo '<p/>';
echo 'Model name of device: ';
echo GetCapability('model_name');
echo '<p/>';
$caps = GetCapabilities(array('brand_name', 'model_name'));
echo 'Capabilities as an array: <p/>';
foreach ($caps as $cap)
    echo "$cap <br/> ";
This will produce this webpage: And that’s it! Your very own piece of cloud. All you need to do is supply a capability to check. There are hundreds that can be used. Here's just a few of them:is_wireless_device (refers to all mobile devices, phones, tablets, portable consoles)
pointing_method (tells you if the input method is a touchscreen or a keypad etc)
device_os (tells you what OS is being, Android, iOS and so on)You can download the PHP code here.

Coming soon, accessing the mobile detection service in Java.

Comments (0)

Author: Anonym

Categories: Development




.NET 4G 51Degrees 5G Acer Adform Adtech Advertising Afilias Alcatel Amazon AMP Analysis Analytics Android Apache API Apple Asian Market ASP.NET Asus Blackberry Browser C C# Centro Chrome chromium Client hints Cloud CMS CPU CSS3 Data Data Blog Data File Daydream Design Detection Developers Device Device Data Device Detection Device Intelligence Device Models Device property DeviceAtlas Disney dmexco DotNetNuke Download ebay Ericsson Event Facebook Firefox Foundation Framework Galaxy git repositories Google Google Analytics Google Daydream GPU GSMA Guess HAProxy Hash Trie HTC HTML5 HTTP HTTP Headers Huawei Infinix Ingeniux Internet usage iOS iOS 13 ipad iPadOS iPhone iPhone 11 Java Javascript Kentico LG Liferay LTE m.dot Memory Memory leak Meta Data Microsoft Mobile Mobile Analysis Mobile Analytics Mobile Devices Mobile Marketing Mixer Motorola Mozilla MWC MWC 2017 MWC16 Native Apps NET New Release News Nexus NFC NGINX Nokia OnePlus 5 Opera Operating System Oppo Optimisation OS Patent Performance PHP Press Release Price Band programmatic Publishers Python Redirection Research Responsive Images Responsive web design RESS Review RTB RWD Safari Samsung Scala ScientiaMobile SEO Server Server-side optimisation Seznam.cz Sitecore Smart TV Smartphone Smartwatches Sony Swedish Beers Tablet Tencent Testing Tips Tutorial Umbraco Update User Agent User-Agent Valgrind Varnish Varnish Cache Video Vodafone VoLTE Web Web Apps Web content management Webtrekk White Paper Widgets WiFi Windows WURFL Xiaomi Xperia ZTE