Mobile Detection with the Zend framework.

Published February 16, 2010 by Richard

Today’s quick tutorial is from Peter Johnson. Thanks Peter, take it away.
—–

Getting up and running with Handset Detection under Zend Framework is Quick & Easy.

To start performing detections you’ll need to download the latest PHP API kit from http://www.handsetdetection.com/resources/api-kits

Extract the whole hdapi folder into your Zend library folder, mine is /application/library, it should be named /application/library/hdapi

Copy your hdconfig.ini file from the kit into your public folder, mine is /public, it should be named /public/hdconfig.ini

Now you should be able to start using the API directly, but to simpify the process even further you can use this wrapper class.
Create a file called ‘HandsetDetectionService.php’ in your Zend library folder, mine is /application/library, it should be named /application/library/HandsetDetectionService.php

< ?php       class HandsetDetectionService   {       private $_instance;       private $_caching = true;       private $_hdapi_path;              public function __construct()       {           $this->_importHdApi();
          $this->_instance = new HandsetDetection();
          $this->_instance->setTimeout( 10 );
          $this->_instance->detectInit();
      }
             public function isMobile()
      {
          if( !$_caching ){ $this->_instance->clearCache(); }
          return $this->_instance->ismobile();
      }
             public function mobileRedirect( $url = "http://m.google.com" )
      {
          $this->_instance->setMobileSite( $url );
          if( $this->isMobile() ) $this->_instance->redirectToMobileSite();
      }
             public function detect()
      {
          if( !$_caching ){ $this->_instance->clearCache(); }
          $this->_instance->detect();
          return $this->_instance->getDetect();
      }
             public function vendorList()
      {
          if( !$_caching ){ $this->_instance->clearCache(); }
          $this->_instance->vendor();
          return $this->_instance->getVendor();
      }
             public function modelList( $vendor_name )
      {
          if( !$_caching ){ $this->_instance->clearCache(); }
          $this->_instance->model( $vendor_name );
          return $this->_instance->getModel();
      }
             public function getError()
      {
          $error = $this->_instance->getError();
          return ( $error == 'Not Found' ? '' : $error );
      }
             private function _importHdApi()
      {
          $this->_hdapi_path = APPLICATION_PATH . DIRECTORY_SEPARATOR . 'library' .
              DIRECTORY_SEPARATOR . 'hdapi' . DIRECTORY_SEPARATOR;
                                             require_once( $this->_hdapi_path . 'class.wsse.php' );
          if (!function_exists('json_encode')) require_once( $this->_hdapi_path . 'json.php');
          require_once( $this->_hdapi_path . 'hdbase.php' );
      }
         }
?>

Now you can use this code in any controller to test out the API:

< ?php   // Setup Connection   $hd = new HandsetDetectionService();   // Perform a Basic Detect, Just to see if it's a Mobile Handset   $ismobile = $hd->isMobile();
  $error = $hd->getError();

  // Output Some Data
  if( $error ) echo "An Error Occured: $error";
  elseif ( $ismobile )
  {
      // Do a Full Detection, Only needed if you want the Device details
      // ..such as Model Name or Handset Capabilities.
      if( $ismobile ) $detection = $hd->detect();

       echo "It's a Mobile Handset! - " . @$detection['product_info']['brand_name'] . " " . @$detection['product_info']['model_name'];
       echo ""; print_r( $detection ); echo "";
  }
  else echo "Not a Mobile Handset.";

  // Query for a list of Vendors & Models
  if( !$error )
  {
      echo "Vendor List";
      echo ""; print_r( $hd->vendorList() ); echo "";
      echo "Models from 'Apple'";
      echo ""; print_r( $hd->modelList('Apple') ); echo "";
  }
?>

or, if you just want to do a basic Mobile Redirect, use this:

< ?php   $hd = new HandsetDetectionService();   $hd->mobileRedirect( 'http://m.digg.com' );
?>

Note: If Zend is having trouble loading the HandsetDetectionService class, you probably have Autoloading disabled and you’ll need to include that file manually, to do this add the following before you call ‘new HandsetDetectionService()’:

require_once( $this->_hdapi_path = APPLICATION_PATH . DIRECTORY_SEPARATOR . 'library' . DIRECTORY_SEPARATOR . 'HandsetDetectionService.php' );

Related posts:

  1. Firefox User-Agent-Switcher – Excellent for mobile website testing!



Posted In Mobile Web | 1 Comment

Rolling Database Upgrade

Published February 15, 2010 by Richard

Just a quick note to let you know that we’re upgrading database servers today.

The blog and forum will be running on reduced effectiveness, detection services will not be effected.

We now return you to your regularly scheduled program & have a nice day. :-)

Update : Detection services suffered 30 minutes of intermittent outages over the course of 3.5 hours while we diagnosed a load balancing problem. We’re disappointed that the service suffered and apologies if you were affected.


Posted In Support | 0 Comments

Our new helpdesk is Zendesk

Published February 8, 2010 by Richard

Were moving to Zendesk

Quietly, in the background, since Christmas, we’ve been analyzing, installing and generally fooling around with nearly a dozen leading help desk systems. We’ve been installing, tweaking, breaking, emailing, emailing and emailing (bueller…). Most have their strengths and weaknesses, some just seemed to have weaknesses. The most important thing is that we’ve chosen a winner, and that’s Zendesk.

Zendesk just had more awesome, so in the end it was an easy decision. We’re excited to be plugging Zendesk into Handset Detection and we’ll be switching on Zendesk Wednesday Feb 10 in the morning, after coffee (GMT + 11).

What does this mean to you ?

Some of you will be in the middle of an email support conversation. When we flick the switch you’ll get a trouble ticket email saying ‘Thanks for your request …’, which might seem weird because, well, obviously we know about your request, but Zendesk doesnt. So just wanted to point that out.

We’ll also be moving our forum to Zendesk on Tuesday the 16th (GMT +11). The old forum will linger on for a while, we’ll move the more interesting posts across and then switch the old forum off.

We hope the new helpdesk is a great experience and, as always, let us know how you get on.


Posted In Updates | 0 Comments

Handset Detection – Jan/Feb 2010 updates

Published February 3, 2010 by Richard

A few weeks ago we launched our new green themed website along with a raft of changes and tweaks. Here’s a quick overview of the new stuff.

Profile

Deep down, at the bottom of the Profile Settings page, you can find Quota Alerts. Quota alerts will notify you by email as your account approaches its monthly detection quota.  Alerts default to On and are set at 90% threshold. Checks are performed hourly, so if you’re in the middle of a super large volume spike then its best to drop your ideal threshold by a few percent.

Quota Alerts

Quota Alerts

We’ve also made it super easy to configure the API kits with a configuration file you can download straight from your Profile page (Its down the bottom in the Get Connected section, Note : The PHP API kit only at the moment, coming to the other kits shortly).

Analytics

We’ve improved device segmentation and added Operating Systems and Screen Size as sorting options. Click on the relevant links, ie the screen size or the operating system for more information and a full device breakdown.

Operating System Device Segmentation

Operating System Device Segmentation

Screen Size Device Segmentation

Screen Size Device Segmentation

Section overviews now compare the top 5 devices. Click the check boxes in the legend to toggle graphing on or off for any particular data set (like devices / countries etc..). Plus snazzy new graphs individual devices and aggregate data sets.

Handset History

Handset History

Detection History

Detection History

We’re Listening

The single largest piece of feedback we received last year was, This thing looks great, but how do i use it ?

We’ve taken many of your comments and suggestions on board. We’ve re-designed, re-branded and re-jigged everything to make Handset Detection an easy and enjoyable experience. We hope you like the new look but more importantly that you find what you need in places you expect.

If you have any comments and suggestions then please let us know, below. We’re all ears.

Related posts:

  1. Updated PHP API Kit Released (v31)
  2. Pricing Released
  3. New Website Goes Live



Posted In Release | 0 Comments