Client
in package
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.
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):
- Set your DeviceAtlas licence key to the class constant LICENCE_KEY in this file.
- Include this file into your application.
- To get the best results include the "DeviceAtlas Client Side Component"
into your page (that is all you need to do).
When available Client will automatically find and use
the client side properties.
- Get the data.
try { $data = Client::getDeviceData(); } catch (Exception $x) { // handle exceptions ... }
// legacy: (class constant DEBUG = false) // the errors have to be checked manually from the returned array $data = Client::getDeviceData();
The returned data is as:
$data[Client::KEY_ERROR] will exist if errors happened while fetching data $data[Client::KEY_PROPERTIES] an array of device properties $data[Client::KEY_USERAGENT] the user-agent string that was used to get the properties $data[Client::KEY_SOURCE] shows where the data came from and is one of: Client::SOURCE_FILE_CACHE Client::SOURCE_CLOUD Client::SOURCE_NONE
// get properties for a user-agent try { $data = Client::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 = Client::getDeviceData($headers); } catch (Exception $x) { // handle exceptions ... }
Notes:
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.
Tags
Table of Contents
Constants
- API_VERSION = '2.1'
- DeviceAtlas Cloud API version
- CLIENT_COOKIE_HEADER = 'Client-Properties'
- For API internal usage
- CLOUD_PATH = '/v1/detect/properties?licencekey=%s&useragent=%s'
- For API internal usage
- DA_HEADER_PREFIX = 'X-DA-'
- For API internal usage
- FAILOVER_CONTINUE = 2
- Action to be taken after an end-point responds: If the error controller returns this the fail-over mechanism must try the next end-point
- FAILOVER_NOT_REQUIRED = 0
- Action to be taken after an end-point responds: If an-endpoint response was fine
- FAILOVER_STOP = 1
- Action to be taken after an end-point responds: If the error controller returns this the fail-over mechanism must stop trying the next end-point
- 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
- MIN_PHP_VERSION = '5.2.1'
- Min DeviceAtlas Cloud API required PHP version
- SOURCE_CLOUD = 'cloud'
- Device data source value set to KEY_SOURCE - device data source was cloud service
- SOURCE_COOKIE = 'cookie'
- Device data source value set to KEY_SOURCE - device data source was cookie
- 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
- TEST_USERAGENT = 'Mozilla/5.0 (Linux; U; Android 2.3.3; en-gb; GT-I9100 Build/GINGERBREAD) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1'
Properties
- $autoServerRanking : bool
- true: server preference is decided by the API (faster server is preferred) false: server preference is $SERVERS sort order (top server is preferred)
- $autoServerRankingLifetimeMinutes : int
- Server preferred list will be updated when older than this amount of minutes
- $autoServerRankingMaxFailures : int
- When ranking servers, if a server fails more than this number phase it out
- $autoServerRankingNumRequests : int
- Number of requests to send when testing server latency
- $cacheCookieName : string
- Cookie name for items cached in cookies
- $cacheDir : string
- The file path that cache items will be stored at. If null the system temp dir is used
- $cacheExpirySeconds : int
- Cache expire (for both file and cookie) 2592000 = 30 days in seconds
- $cacheName : string
- File cache > directory name
- $calledServers : mixed
- $clientCookieName : string
- Name of the cookie created by "DeviceAtlas Client Side Component
- $fatalErrors : mixed
- $headers : mixed
- $lastUsedCloudUrl : mixed
- $licenceKey : string
- $rankingStatus : mixed
- $selfAutoRanking : mixed
- $serverPhaseoutLifetimeMinutes : int
- Auto ranking = false > if top server fails it will be phased out for this amount of minutes
- $servers : array<string|int, mixed>
- List of cloud service provider end-points.
- $timeoutSeconds : int
- Max time (seconds) to wait for each cloud server to give service
- $useClientCookie : bool
- Use device data which is created by the DeviceAtlas Client Side Component if present
- $useCookieCache : bool
- Cache cloud results in cookies
- $useFileCache : bool
- Cache cloud results in files
Methods
- getAutoServerRankingLifetimeMinutes() : int
- getAutoServerRankingMaxFailures() : int
- getAutoServerRankingNumRequests() : int
- getCacheBasePath() : string
- Get the directory path in which the DeviceAtlas CloudApi puts cache files in (device data cache and server fail-over list).
- getCacheCookieName() : string
- getCacheDir() : string
- getCacheExpirySeconds() : int
- getCacheName() : string
- getCalledServers() : array<string|int, mixed>
- getClientCookieName() : string
- getCloudUrl() : string
- Get the URL of the last DeviceAtlas cloud end-point which was called to get device properties.
- getDaClientUserAgent() : mixed
- getDeviceData() : array<string|int, mixed>
- getFatalErrors() : mixed
- getHeaders() : mixed
- getLastUsedCloudUrl() : mixed
- getLicenceKey() : string
- getRankedServers() : array<string|int, mixed>
- Get DeviceAtlas cloud end-point list in the same order used by the API.
- getRankingStatus() : mixed
- getSelfAutoRanking() : mixed
- getServerPhaseoutLifetimeMinutes() : int
- getServers() : array<string|int, mixed>
- getServersLatencies() : array<string|int, mixed>
- Get a list of cloud end-points and their service latencies.
- getTimeoutSeconds() : int
- isAutoServerRanking() : bool
- isUseClientCookie() : bool
- isUseCookieCache() : bool
- isUseFileCache() : bool
- rankServers() : array<string|int, mixed>
- If auto-ranking is on then rank the DeviceAtlas cloud end-points and put in cache.
- setAutoServerRanking() : mixed
- setAutoServerRankingLifetimeMinutes() : mixed
- setAutoServerRankingMaxFailures() : mixed
- setAutoServerRankingNumRequests() : mixed
- setCacheCookieName() : mixed
- setCacheDir() : mixed
- setCacheExpirySeconds() : mixed
- setCacheName() : mixed
- setClientCookieName() : mixed
- setLicenceKey() : mixed
- setServerPhaseoutLifetimeMinutes() : mixed
- setServers() : mixed
- setTimeoutSeconds() : mixed
- setUseClientCookie() : mixed
- setUseCookieCache() : mixed
- setUseFileCache() : mixed
- __rankServers() : mixed
- servers=null rank and cache end-points servers=array cache servers (for updating the manual fail-over list)
- getNormalisedHeaders() : mixed
- normalize the HTTP header keys
- prepareHeaders() : string
- Prepare cloud request headers which can help the detection
- connectCloud() : array<string|int, mixed>
- connect to a cloud server and get device data, return data or null
- errorControler() : array<string|int, mixed>
- when an end-point returns an array (action, error-message) this method will check it
- getCloudService() : mixed
- get data from the DeviceAtlas Cloud service
- getCookieCache() : mixed
- COOKIE CACHE > if device data is cached in a cookie then fetch data cookie cache remains in the API only to be compatible with older versions
- getDaHeaders() : mixed
- getDeviceCachePath() : mixed
- FILE CACHE > get the device data cache file-path
- getFileCache() : mixed
- FILE CACHE > if device data is cached in a file then fetch data
- getServerLatency() : mixed
- send request(s) to a DA cloud server and return the latencies
- rankSort() : mixed
- sort by latency
- setCookieCache() : mixed
- COOKIE CACHE > cache device data into two cookies on the client cookie cache remains in the API only to be compatible with older versions
- setFileCache() : mixed
- FILE CACHE > cache device data into a file
- writeCacheToFile() : mixed
- lock cache file and write to it
Constants
API_VERSION
DeviceAtlas Cloud API version
public
mixed
API_VERSION
= '2.1'
CLIENT_COOKIE_HEADER
For API internal usage
public
mixed
CLIENT_COOKIE_HEADER
= 'Client-Properties'
CLOUD_PATH
For API internal usage
public
mixed
CLOUD_PATH
= '/v1/detect/properties?licencekey=%s&useragent=%s'
DA_HEADER_PREFIX
For API internal usage
public
mixed
DA_HEADER_PREFIX
= 'X-DA-'
FAILOVER_CONTINUE
Action to be taken after an end-point responds: If the error controller returns this the fail-over mechanism must try the next end-point
public
mixed
FAILOVER_CONTINUE
= 2
FAILOVER_NOT_REQUIRED
Action to be taken after an end-point responds: If an-endpoint response was fine
public
mixed
FAILOVER_NOT_REQUIRED
= 0
FAILOVER_STOP
Action to be taken after an end-point responds: If the error controller returns this the fail-over mechanism must stop trying the next end-point
public
mixed
FAILOVER_STOP
= 1
KEY_ERROR
A key of array returned by getDeviceData() - the error message is set to this key
public
mixed
KEY_ERROR
= 'error'
KEY_PROPERTIES
A key of array returned by getDeviceData() - the device properties are set to this key
public
mixed
KEY_PROPERTIES
= 'properties'
KEY_SOURCE
A key of array returned by getDeviceData() - the source of data is set to this key
public
mixed
KEY_SOURCE
= 'source'
KEY_USERAGENT
A key of array returned by getDeviceData() - the user-agent is set to this key
public
mixed
KEY_USERAGENT
= 'useragent'
MIN_PHP_VERSION
Min DeviceAtlas Cloud API required PHP version
public
mixed
MIN_PHP_VERSION
= '5.2.1'
SOURCE_CLOUD
Device data source value set to KEY_SOURCE - device data source was cloud service
public
mixed
SOURCE_CLOUD
= 'cloud'
SOURCE_COOKIE
Device data source value set to KEY_SOURCE - device data source was cookie
public
mixed
SOURCE_COOKIE
= 'cookie'
SOURCE_FILE_CACHE
Device data source value set to KEY_SOURCE - device data source was file cache
public
mixed
SOURCE_FILE_CACHE
= 'file'
SOURCE_NONE
Device data source value set to KEY_SOURCE - no device data, indicates an error
public
mixed
SOURCE_NONE
= 'none'
TEST_USERAGENT
public
mixed
TEST_USERAGENT
= 'Mozilla/5.0 (Linux; U; Android 2.3.3; en-gb; GT-I9100 Build/GINGERBREAD) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1'
Properties
$autoServerRanking
true: server preference is decided by the API (faster server is preferred) false: server preference is $SERVERS sort order (top server is preferred)
private
bool
$autoServerRanking
= true
$autoServerRankingLifetimeMinutes
Server preferred list will be updated when older than this amount of minutes
private
int
$autoServerRankingLifetimeMinutes
= 1440
$autoServerRankingMaxFailures
When ranking servers, if a server fails more than this number phase it out
private
int
$autoServerRankingMaxFailures
= 1
$autoServerRankingNumRequests
Number of requests to send when testing server latency
private
int
$autoServerRankingNumRequests
= 3
$cacheCookieName
Cookie name for items cached in cookies
private
string
$cacheCookieName
= 'DACACHE'
$cacheDir
The file path that cache items will be stored at. If null the system temp dir is used
private
string
$cacheDir
$cacheExpirySeconds
Cache expire (for both file and cookie) 2592000 = 30 days in seconds
private
int
$cacheExpirySeconds
= 2592000
$cacheName
File cache > directory name
private
string
$cacheName
= 'deviceatlas_cache'
$calledServers
private
mixed
$calledServers
= array()
$clientCookieName
Name of the cookie created by "DeviceAtlas Client Side Component
private
string
$clientCookieName
= 'DAPROPS'
$fatalErrors
private
mixed
$fatalErrors
$headers
private
mixed
$headers
$lastUsedCloudUrl
private
mixed
$lastUsedCloudUrl
$licenceKey
private
string
$licenceKey
$rankingStatus
private
mixed
$rankingStatus
$selfAutoRanking
private
mixed
$selfAutoRanking
$serverPhaseoutLifetimeMinutes
Auto ranking = false > if top server fails it will be phased out for this amount of minutes
private
int
$serverPhaseoutLifetimeMinutes
= 1440
$servers
List of cloud service provider end-points.
private
array<string|int, mixed>
$servers
= array(array('host' => 'region0.deviceatlascloud.com', 'port' => 80), array('host' => 'region1.deviceatlascloud.com', 'port' => 80), array('host' => 'region2.deviceatlascloud.com', 'port' => 80), array('host' => 'region3.deviceatlascloud.com', 'port' => 80))
The order of this list is par with end-point preference
$timeoutSeconds
Max time (seconds) to wait for each cloud server to give service
private
int
$timeoutSeconds
= 2
$useClientCookie
Use device data which is created by the DeviceAtlas Client Side Component if present
private
bool
$useClientCookie
= true
$useCookieCache
Cache cloud results in cookies
private
bool
$useCookieCache
= false
$useFileCache
Cache cloud results in files
private
bool
$useFileCache
= true
Methods
getAutoServerRankingLifetimeMinutes()
public
getAutoServerRankingLifetimeMinutes() : int
Return values
intgetAutoServerRankingMaxFailures()
public
getAutoServerRankingMaxFailures() : int
Return values
intgetAutoServerRankingNumRequests()
public
getAutoServerRankingNumRequests() : int
Return values
intgetCacheBasePath()
Get the directory path in which the DeviceAtlas CloudApi puts cache files in (device data cache and server fail-over list).
public
getCacheBasePath() : string
Return values
string —The cache directory
getCacheCookieName()
public
getCacheCookieName() : string
Return values
stringgetCacheDir()
public
getCacheDir() : string
Return values
stringgetCacheExpirySeconds()
public
getCacheExpirySeconds() : int
Return values
intgetCacheName()
public
getCacheName() : string
Return values
stringgetCalledServers()
public
getCalledServers() : array<string|int, mixed>
Return values
array<string|int, mixed>getClientCookieName()
public
getClientCookieName() : string
Return values
stringgetCloudUrl()
Get the URL of the last DeviceAtlas cloud end-point which was called to get device properties.
public
getCloudUrl() : string
Return values
string —The end-point URL, null=no call to a cloud end-point was made probably because the data was fetched from cache.
getDaClientUserAgent()
public
static getDaClientUserAgent() : mixed
getDeviceData()
public
getDeviceData([mixed $param = false ]) : array<string|int, mixed>
Parameters
- $param : mixed = false
Tags
Return values
array<string|int, mixed> —{ Client::KEY_USERAGENT: "UA" | null, Client::KEY_SOURCE: "data source", Client::KEY_PROPERTIES: {"propertyName": "PropertyVal",} | null, Client::KEY_ERROR: err-msg-string | null, }
getFatalErrors()
public
getFatalErrors() : mixed
getHeaders()
public
getHeaders() : mixed
getLastUsedCloudUrl()
public
getLastUsedCloudUrl() : mixed
getLicenceKey()
public
getLicenceKey() : string
Return values
stringgetRankedServers()
Get DeviceAtlas cloud end-point list in the same order used by the API.
public
getRankedServers() : array<string|int, mixed>
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.
Return values
array<string|int, mixed> —DeviceAtlas cloud End-pont list {host: server-address, port: server-port}
getRankingStatus()
public
getRankingStatus() : mixed
getSelfAutoRanking()
public
getSelfAutoRanking() : mixed
getServerPhaseoutLifetimeMinutes()
public
getServerPhaseoutLifetimeMinutes() : int
Return values
intgetServers()
public
getServers() : array<string|int, mixed>
Return values
array<string|int, mixed>getServersLatencies()
Get a list of cloud end-points and their service latencies.
public
getServersLatencies([mixed $numRequests = null ]) : array<string|int, mixed>
Parameters
- $numRequests : mixed = null
Return values
array<string|int, mixed> —Cloud end-point info {{avg:, latencies:, host:, port:},}
getTimeoutSeconds()
public
getTimeoutSeconds() : int
Return values
intisAutoServerRanking()
public
isAutoServerRanking() : bool
Return values
boolisUseClientCookie()
public
isUseClientCookie() : bool
Return values
boolisUseCookieCache()
public
isUseCookieCache() : bool
Return values
boolisUseFileCache()
public
isUseFileCache() : bool
Return values
boolrankServers()
If auto-ranking is on then rank the DeviceAtlas cloud end-points and put in cache.
public
rankServers() : array<string|int, mixed>
Tags
Return values
array<string|int, mixed> —the ranked end-point list or null=did not rank successfully
setAutoServerRanking()
public
setAutoServerRanking(bool $autoServerRanking) : mixed
Parameters
- $autoServerRanking : bool
setAutoServerRankingLifetimeMinutes()
public
setAutoServerRankingLifetimeMinutes(int $autoServerRankingLifetimeMinutes) : mixed
Parameters
- $autoServerRankingLifetimeMinutes : int
setAutoServerRankingMaxFailures()
public
setAutoServerRankingMaxFailures(int $autoServerRankingMaxFailures) : mixed
Parameters
- $autoServerRankingMaxFailures : int
setAutoServerRankingNumRequests()
public
setAutoServerRankingNumRequests(int $autoServerRankingNumRequests) : mixed
Parameters
- $autoServerRankingNumRequests : int
setCacheCookieName()
public
setCacheCookieName(string $cacheCookieName) : mixed
Parameters
- $cacheCookieName : string
setCacheDir()
public
setCacheDir(string $cacheDir) : mixed
Parameters
- $cacheDir : string
setCacheExpirySeconds()
public
setCacheExpirySeconds(int $cacheExpirySeconds) : mixed
Parameters
- $cacheExpirySeconds : int
setCacheName()
public
setCacheName(string $cacheName) : mixed
Parameters
- $cacheName : string
setClientCookieName()
public
setClientCookieName(string $clientCookieName) : mixed
Parameters
- $clientCookieName : string
setLicenceKey()
public
setLicenceKey(string $licenceKey) : mixed
Parameters
- $licenceKey : string
setServerPhaseoutLifetimeMinutes()
public
setServerPhaseoutLifetimeMinutes(int $serverPhaseoutLifetimeMinutes) : mixed
Parameters
- $serverPhaseoutLifetimeMinutes : int
setServers()
public
setServers(array<string|int, mixed> $servers) : mixed
Parameters
- $servers : array<string|int, mixed>
setTimeoutSeconds()
public
setTimeoutSeconds(int $timeoutSeconds) : mixed
Parameters
- $timeoutSeconds : int
setUseClientCookie()
public
setUseClientCookie(bool $useClientCookie) : mixed
Parameters
- $useClientCookie : bool
setUseCookieCache()
public
setUseCookieCache(bool $useCookieCache) : mixed
Parameters
- $useCookieCache : bool
setUseFileCache()
public
setUseFileCache(bool $useFileCache) : mixed
Parameters
- $useFileCache : bool
__rankServers()
servers=null rank and cache end-points servers=array cache servers (for updating the manual fail-over list)
protected
__rankServers([mixed $servers = array() ]) : mixed
Parameters
- $servers : mixed = array()
getNormalisedHeaders()
normalize the HTTP header keys
protected
getNormalisedHeaders([mixed $setHeaders = null ]) : mixed
Parameters
- $setHeaders : mixed = null
prepareHeaders()
Prepare cloud request headers which can help the detection
protected
prepareHeaders(string|null $clientSideCookie) : string
Parameters
- $clientSideCookie : string|null
Return values
stringconnectCloud()
connect to a cloud server and get device data, return data or null
private
connectCloud( $server, $headers, &$errors) : array<string|int, mixed>
Parameters
Return values
array<string|int, mixed> —(action, props) & errors byref
errorControler()
when an end-point returns an array (action, error-message) this method will check it
private
static errorControler( $server, $status, $msg) : array<string|int, mixed>
Parameters
Return values
array<string|int, mixed>getCloudService()
get data from the DeviceAtlas Cloud service
private
getCloudService(mixed $headers, mixed $userAgent) : mixed
Parameters
- $headers : mixed
- $userAgent : mixed
getCookieCache()
COOKIE CACHE > if device data is cached in a cookie then fetch data cookie cache remains in the API only to be compatible with older versions
private
getCookieCache(mixed $cookie) : mixed
Parameters
- $cookie : mixed
getDaHeaders()
private
getDaHeaders() : mixed
getDeviceCachePath()
FILE CACHE > get the device data cache file-path
private
getDeviceCachePath(mixed $userAgent, mixed $cookie) : mixed
Parameters
- $userAgent : mixed
- $cookie : mixed
getFileCache()
FILE CACHE > if device data is cached in a file then fetch data
private
getFileCache(mixed $userAgent, mixed $cookie) : mixed
Parameters
- $userAgent : mixed
- $cookie : mixed
getServerLatency()
send request(s) to a DA cloud server and return the latencies
private
getServerLatency(mixed $server, mixed $numRequests) : mixed
Parameters
- $server : mixed
- $numRequests : mixed
rankSort()
sort by latency
private
static rankSort(mixed $a, mixed $b) : mixed
Parameters
- $a : mixed
- $b : mixed
setCookieCache()
COOKIE CACHE > cache device data into two cookies on the client cookie cache remains in the API only to be compatible with older versions
private
setCookieCache(mixed $cookie, mixed $deviceData) : mixed
Parameters
- $cookie : mixed
- $deviceData : mixed
setFileCache()
FILE CACHE > cache device data into a file
private
setFileCache(mixed $userAgent, mixed $cookie, mixed $deviceData) : mixed
Parameters
- $userAgent : mixed
- $cookie : mixed
- $deviceData : mixed
writeCacheToFile()
lock cache file and write to it
private
static writeCacheToFile(mixed $path, mixed $data[, mixed $errMsg = null ]) : mixed
Parameters
- $path : mixed
- $data : mixed
- $errMsg : mixed = null