Source for file TeraWurflRemoteClient.php
Documentation is available at TeraWurflRemoteClient.php
* Tera_WURFL - PHP MySQL driven WURFL
* Tera-WURFL was written by Steve Kamerman, and is based on the
* Java WURFL Evolution package by Luca Passani and WURFL PHP Tools by Andrea Trassati.
* This version uses a MySQL database to store the entire WURFL file, multiple patch
* files, and a persistent caching mechanism to provide extreme performance increases.
* @package TeraWurflRemoteClient
* @author Steve Kamerman <stevekamerman AT gmail.com>
* @version Stable 2.1.3 $Date: 2010/09/18 15:43:21
* @license http://www.mozilla.org/MPL/ MPL Vesion 1.1
* Tera-WURFL remote webservice client for PHP
* @package TeraWurflRemoteClient
* XML Data Format - this should only be used to communicate with Tera-WURFL 2.1.1 and older
public static $FORMAT_XML = 'xml';
* The JSON Data Format is the default transport for Tera-WURFL 2.1.2 and newer due to it's smaller size
* and better performance with the builtin PHP functions
public static $FORMAT_JSON = 'json';
* If you try to use a capability that has not been retrieved yet and this is set to true,
* it will generate another request to the webservice and retrieve this capability automatically.
* Flattened version of Tera-WURFL's capabilities array, containing only capability names and values.
* Since it is 'Flattened', there a no groups in this array, just individual capabilities.
* Array of errors that were encountered while processing the request and/or response.
* The HTTP Headers that Tera-WURFL will look through to find the best User Agent, if one is not specified
public static $userAgentHeaders = array(
'HTTP_X_DEVICE_USER_AGENT',
'HTTP_X_ORIGINAL_USER_AGENT',
'HTTP_X_OPERAMINI_PHONE_UA',
* Creates a TeraWurflRemoteClient object. NOTE: in Tera-WURFL 2.1.2 the default data format is JSON.
* This format is not supported in Tera-WURFL 2.1.1 or earlier, so if you must use this client with
* an earlier version of the server, set the second parameter to TeraWurflRemoteClient::$FORMAT_XML
* @param String URL to the master Tera-WURFL Server's webservice.php
* @param String TeraWurflRemoteClient::$FORMAT_JSON or TeraWurflRemoteClient::$FORMAT_XML
public function __construct($TeraWurflWebserviceURL,$data_format= 'json',$timeout= 1){
if(!self::validURL($TeraWurflWebserviceURL)){
throw new Exception("TeraWurflRemoteClient Error: the specified webservice URL is invalid. Please make sure you pass the full url to Tera-WURFL's webservice.php.");
* Get the requested capabilities from Tera-WURFL for the given user agent
* @param String HTTP User Agent of the device being detected
* @param Array Array of capabilities that you would like to retrieve
$this->userAgent = (is_null($userAgent))? self::getUserAgent(): $userAgent;
$uri = $this->webserviceUrl . (strpos($this->webserviceUrl,'?')=== false? '?': '&')
. 'ua=' . urlencode($this->userAgent)
. '&format=' . $this->format
. '&search=' . implode('|',$capabilities);
$this->callTeraWurfl($uri);
$this->loadCapabilities();
* Maintains backwards compatibility with Tera-WURFL <= 2.1.2. This function is an
* alias for TeraWurflRemoteClient::getDeviceCapabilitiesFromAgent()
* @param String HTTP User Agent of the device being detected
* @param Array Array of capabilities that you would like to retrieve
return $this->getDeviceCapabilitiesFromAgent($userAgent,$capabilities);
* Returns the value of the requested capability
* @param String The WURFL capability you are looking for (e.g. "is_wireless_device")
* @return Mixed String, Numeric, Bool
* Get the version of the Tera-WURFL Remote Client (this file)
* Get the version of the Tera-WURFL Webservice (webservice.php on server). This is only available
* after a query has been made since it is returned in the response.
* Get the date that the Tera-WURFL was last updated. This is only available
* after a query has been made since it is returned in the response.
* Make the webservice call to the server using the GET method and load the response.
* @param String The URI of the master server
$context_options = array(
'user_agent' => 'Tera-WURFL/RemoteClient v'. $this->clientVersion,
$context_options['http']['timeout'] = $this->timeout;
$data = file_get_contents($uri,false,$context);
// Trigger the catch block
throw new Exception("foo");
throw new Exception("foo");
// Can't use builtin logging here through Tera-WURFL since it is on the client, not the server
throw new Exception("TeraWurflRemoteClient Error: Could not query Tera-WURFL master server.");
* Parse the response into the capabilities array
$this->apiVersion = $this->json['apiVersion'];
$this->apiVersion = $this->xml->device['apiVersion'];
foreach($this->xml->device->capability as $cap){
$this->capabilities[(string) $cap['name']] = self::niceCast((string) $cap['value']);
* Parse the response's errors into the errors array
$this->errors &= $this->json['errors'];
foreach($this->xml->errors->error as $error){
$this->errors[(string) $error['name']]=(string) $error['description'];
* Cast strings into proper variable types, i.e. 'true' into true
* @return Mixed String, Bool, Float
protected static function niceCast($value){
if($value === 'true')$value= true;
if($value === 'false')$value= false;
// Clean Numeric values by loosely comparing the (float) to the (string)
if(strcmp($value,$numval)== 0)$value= $numval;
protected static function validURL($url){
if(preg_match('/^(http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/',$url)) return true;
* Return the requesting client's User Agent
$userAgent = $_GET['UA'];
foreach(self::$userAgentHeaders as $header){
if(array_key_exists($header,$source) && $source[$header]){
$userAgent = $source[$header];
|