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

1 Comment

Sergiu Neamt
Oct 20, 2010

In class HandsetDetectionService the variable $_caching from every method is local to method, not class variabile. Should be $this->_caching.

Post a Comment

If you’d like a picture to show up by your name, Get a Gravatar