TOP

DeviceAtlasCloudClient

DeviceAtlas Cloud API. This client library can be used to easily get device data from the DeviceAtlas Cloud service. To reduce cloud calls and improve performance the API locally caches the data returned from the cloud service. This API caches data on the disk.
If the cache path is not writable the APin the I will not proceed even if the device cache is turned off.
It is recommended to set DEBUG = true during implementation so if the cache path is not writable the errors will not be ignored.
The client is queried by passing either nothing (the request's headers will be used) or by manually passing a collection of HTTP headers or a user-agent string. The device properties will then be returned.

Usage (see the provided examples):
1. Set your DeviceAtlas licence key to the class constant LICENCE_KEY in this file.
2. Include this file into your application.
3. To get the best results include the "DeviceAtlas Client Side Component" into your page (that is all you need to do). When available DeviceAtlasCloudClient will automatically find and use the client side properties.
4. Get the data.
 try {
     $data = DeviceAtlasCloudClient::getDeviceData();
 } catch (Exception $x) {
     // handle exceptions ...
 }
 // legacy: (class constant DEBUG = false)
 // the errors have to be checked manually from the returned array
 $data = DeviceAtlasCloudClient::getDeviceData();
The returned data is as:
 $data[DeviceAtlasCloudClient::KEY_ERROR]
     will exist if errors happened while fetching data
 $data[DeviceAtlasCloudClient::KEY_PROPERTIES]
     an array of device properties
 $data[DeviceAtlasCloudClient::KEY_USERAGENT]
     the user-agent string that was used to get the properties
 $data[DeviceAtlasCloudClient::KEY_SOURCE]
     shows where the data came from and is one of:
     DeviceAtlasCloudClient::SOURCE_FILE_CACHE
     DeviceAtlasCloudClient::SOURCE_CLOUD
     DeviceAtlasCloudClient::SOURCE_NONE
 // get properties for a user-agent
 try {
     $data = DeviceAtlasCloudClient::getDeviceData("user-agent-string ...");
 } catch (Exception $x) {
     // handle exceptions ...
 }
 // get properties for a set of headers
 $headers = array(
     "User-Agent"      => "Mozilla/5.0 (SymbianOS/9.2; ...",
     "X-Profile"       => "http://nds.nokia.com/uaprof/NN95_8GB-1r100.xml",
     "Accept"          => "text/html,text/css,multipart/mixed,application...",
     "Accept-Language" => "en-us,en;q=0.5",
 );
 try {
     $data = DeviceAtlasCloudClient::getDeviceData($headers);
 } catch (Exception $x) {
     // handle exceptions ...
 }
Notes:
1 When in test mode and using cookie cache, changing the user-agent will not update the cookie cache and you have to manually remove the cookie. Because in real life the the cookie is on the system it represents.
2 It is recommended to always use file cache. Using cookie cache will cache device info on the user's browser. You can have both caches on, then if device data is found in the cookie it will be used otherwise if data is cached in the files the file cache will be used.
Source
Client.php
Depends on
Exception
Author
dotMobi

Constants

API_VERSION = 'API_VERSION'

DeviceAtlas Cloud API version

AUTO_SERVER_RANKING_LIFETIME = 1440

Server preferred list will be updated when older than this amount of minutes

AUTO_SERVER_RANKING_MAX_FAILURE = 1

When ranking servers, if a server fails more than this number phase it out

AUTO_SERVER_RANKING_NUM_REQUESTS = 3

Number of requests to send when testing server latency

CACHE_ITEM_EXPIRY_SEC = 2592000

Cache expire (for both file and cookie) 2592000 = 30 days in seconds

CACHE_NAME = 'deviceatlas_cache'

File cache > directory name

CLOUD_SERVICE_TIMEOUT = 2

Max time (seconds) to wait for each cloud server to give service

CUSTOM_CACHE_DIR = '/path/to/your/cache/'

File cache > this is only used if USE_SYSTEM_TEMP_DIR is false

DEBUG_MODE = true

true = throw exceptions on all errors and failures

FAILOVER_NOT_REQUIRED= =

Action to be taken after an end-point responds: If an-endpoint response was fine

KEY_ERROR = 'error'

A key of array returned by getDeviceData() - the error message is set to this key

KEY_PROPERTIES = 'properties'

A key of array returned by getDeviceData() - the device properties are set to this key

KEY_SOURCE = 'source'

A key of array returned by getDeviceData() - the source of data is set to this key

KEY_USERAGENT = 'useragent'

A key of array returned by getDeviceData() - the user-agent is set to this key

LICENCE_KEY = 'ENTER-YOUR-LICENCE-KEY'

Set to your DeviceAtlas licence key

MIN_PHP_VERSION = '5.2.1'

Min DeviceAtlas Cloud API required PHP version

SERVER_PHASEOUT_LIFETIME = 1440

Auto ranking = false > if top server fails it will be phased out for this amount of minutes

SOURCE_CLOUD = 'cloud'

Device data source value set to KEY_SOURCE - device data source was cloud service

SOURCE_FILE_CACHE = 'file'

Device data source value set to KEY_SOURCE - device data source was file cache

SOURCE_NONE = 'none'

Device data source value set to KEY_SOURCE - no device data, indicates an error

USE_SYSTEM_TEMP_DIR = true

File cache > leave as true to put cache in systems default temp directory

Static Methods

getDeviceData

array DeviceAtlasCloudClient::getDeviceData()

Get the device data for the current request from DeviceAtlas Cloud. Once data has been returned from DeviceAtlas Cloud it can be cached locally to speed up subsequent requests. If device data provided by "DeviceAtlas Client Side Component" exists in a cookie then cloud data will be merged with the cookie data. When no parameter is provided the client's headers will be used for detection.

Returns

array
 {
      DeviceAtlasCloudClient::KEY_USERAGENT: "UA" | null,
      DeviceAtlasCloudClient::KEY_SOURCE: "data source",
      DeviceAtlasCloudClient::KEY_PROPERTIES: {"propertyName": "PropertyVal",} | null,
      DeviceAtlasCloudClient::KEY_ERROR: err-msg-string | null,
 }

getCloudUrl

string DeviceAtlasCloudClient::getCloudUrl()

Get the URL of the last DeviceAtlas cloud end-point which was called to get device properties.

Returns

string
The end-point URL, null=no call to a cloud end-point was made probably because the data was fetched from cache.

getCacheBasePath

string DeviceAtlasCloudClient::getCacheBasePath()

Get the directory path in which the DeviceAtlas CloudApi puts cache files in (device data cache and server fail-over list).

Returns

string
The cache directory

getDeviceData

array DeviceAtlasCloudClient::getDeviceData(array headers)

Get the device data for a set of HTTP headers from DeviceAtlas Cloud. Once data has been returned from DeviceAtlas Cloud it can be cached locally to speed up subsequent requests. If device data provided by "DeviceAtlas Client Side Component" exists in a cookie then cloud data will be merged with the cookie data. When no parameter is provided the client's headers will be used for detection.

Arguments

    headers (array)
    The HTTP headers to get properties from

Returns

array
 {
      DeviceAtlasCloudClient::KEY_USERAGENT: "UA" | null,
      DeviceAtlasCloudClient::KEY_SOURCE: "data source",
      DeviceAtlasCloudClient::KEY_PROPERTIES: {"propertyName": "PropertyVal",} | null,
      DeviceAtlasCloudClient::KEY_ERROR: err-msg-string | null,
 }

getDeviceData

array DeviceAtlasCloudClient::getDeviceData(string userAgent)

Get the device data for a user-agent string from DeviceAtlas Cloud. Once data has been returned from DeviceAtlas Cloud it can be cached locally to speed up subsequent requests. If device data provided by "DeviceAtlas Client Side Component" exists in a cookie then cloud data will be merged with the cookie data. When no parameter is provided the client's headers will be used for detection.

Arguments

    userAgent (string)
    The user-agent to get properties from

Returns

array
 {
      DeviceAtlasCloudClient::KEY_USERAGENT: "UA" | null,
      DeviceAtlasCloudClient::KEY_SOURCE: "data source",
      DeviceAtlasCloudClient::KEY_PROPERTIES: {"propertyName": "PropertyVal",} | null,
      DeviceAtlasCloudClient::KEY_ERROR: err-msg-string | null,
 }

getDeviceData

array DeviceAtlasCloudClient::getDeviceData(bool test)

Get the device data for a built in test user-agent.

Arguments

    test (bool)
    true=get properties of a test user-agent

Returns

array
 {
      DeviceAtlasCloudClient::KEY_USERAGENT: "UA" | null,
      DeviceAtlasCloudClient::KEY_SOURCE: "data source",
      DeviceAtlasCloudClient::KEY_PROPERTIES: {"propertyName": "PropertyVal",} | null,
      DeviceAtlasCloudClient::KEY_ERROR: err-msg-string | null,
 }

getServers

array DeviceAtlasCloudClient::getServers()

Get DeviceAtlas cloud end-point list in the same order used by the API. If auto-ranking is on then the ranked end-point list is returned otherwise the manual or the default fail-over list is returned.
If auto-ranking is on and the end-point list cache is invalid or out of date then the end-points will be ranked and cached first.

Returns

array
DeviceAtlas cloud End-pont list {host: server-address, port: server-port}

getServersLatencies

array DeviceAtlasCloudClient::getServersLatencies(
    int numRequests=DeviceAtlasCloudClient::AUTO_SERVER_RANKING_NUM_REQUESTS
)

Get a list of cloud end-points and their service latencies.

Arguments

    numRequests=DeviceAtlasCloudClient::AUTO_SERVER_RANKING_NUM_REQUESTS (int)
    The number of times to send request to an end-point per test

Returns

array
Cloud end-point info {{avg:, latencies:, host:, port:},}

rankServers

array DeviceAtlasCloudClient::rankServers()

If auto-ranking is on then rank the DeviceAtlas cloud end-points and put in cache.

Returns

array
the ranked end-point list or null=did not rank successfully

Throws

  • Exception when unable to create cache file or directory