\DeviceAtlas\CarrierCarrierApiWeb

A small extension to the core CarrierApi to get IP from $_SERVER for current client.

The getProperties() and getProperty() methods in this class extract the most suitable client IP.

Example usage:

$carrierApi = new CarrierApiWeb();
// loading data can be slow - it is recommended wrap the API in a Singleton.
$carrierApi->loadDataFromFile('/path/to/sample.dat');

// get all properties for current client
Properties props = carrierApi.GetProperties(request);

// .... use the properties ....
if (props.ContainsKey("networkOperator") {
   string operatorName = props.Get("networkOperator").AsString();
}

// get a single property
Property countryProp = carrierApi.GetProperty("countryCode");
if (countryProp != null) {
  string countryCode = countryProp.AsString();
}

Summary

Methods
Properties
Constants
__construct()
loadDataFromFile()
getDataFileCopyright()
getDataFileCreationDate()
getDataFileVersion()
getProperties()
getProperty()
getPropertyNames()
getIp()
isPublicIp()
buildDataFile()
downloadAndLoadDataFile()
getDeviceAtlasApiVersion()
downloadAndLoadDataFileFromUrl()
scheduleReloadAndFinishRequest()
reload()
$HEADERS_TO_CHECK
API_VERSION
MIN_PHP_VERSION
dataLoaded()
propertyNameExists()
extractIp()
No protected properties found
N/A
checkReloadSchedule()
callFailureCallback()
getIpFromServer()
$data
$MISSING_DATA_EX
$INVALID_PROP_EX
N/A

Constants

API_VERSION

API_VERSION = '1.2.1'

Carrier API version

MIN_PHP_VERSION

MIN_PHP_VERSION = '5.2.3'

Min PHP version required for this API to work

Properties

$HEADERS_TO_CHECK

$HEADERS_TO_CHECK : 

A list of HTTP headers to choose the original client IP address from. In addition to these the RemoteAddr (REMOTE_ADDR) is also used as a final fallback.

Type

$data

$data : 

Data loaded from the data file

Type

$MISSING_DATA_EX

$MISSING_DATA_EX : 

Error messages

Type

$INVALID_PROP_EX

$INVALID_PROP_EX : 

Error messages

Type

Methods

__construct()

__construct() 

Construct a Carrier API object and load the data file (if path is provided).

Throws

\Exception

If PHP version is less than required

loadDataFromFile()

loadDataFromFile(  $jsonDataFilePath) 

Parameters

$jsonDataFilePath
Warning: count(): Parameter must be an array or an object that implements Countable in /opt/phpdoc/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1275 Warning: count(): Parameter must be an array or an object that implements Countable in /opt/phpdoc/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1275

getDataFileCopyright()

getDataFileCopyright() : string

Returns the data file copyright text.

Warning: count(): Parameter must be an array or an object that implements Countable in /opt/phpdoc/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1275 Warning: count(): Parameter must be an array or an object that implements Countable in /opt/phpdoc/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1275

Returns

string —

the copyright

getDataFileCreationDate()

getDataFileCreationDate() : string

Returns the data file creation date in ISO8601 format.

Warning: count(): Parameter must be an array or an object that implements Countable in /opt/phpdoc/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1275 Warning: count(): Parameter must be an array or an object that implements Countable in /opt/phpdoc/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1275

Returns

string —

the creationDate

getDataFileVersion()

getDataFileVersion() : string

Returns the version of the data file.

Warning: count(): Parameter must be an array or an object that implements Countable in /opt/phpdoc/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1275 Warning: count(): Parameter must be an array or an object that implements Countable in /opt/phpdoc/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1275

Returns

string —

the version

getProperties()

getProperties(  $param = null) : \DeviceAtlas\Properties

Get the Carrier properties for the current client.

Parameters

$param
Warning: count(): Parameter must be an array or an object that implements Countable in /opt/phpdoc/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1275 Warning: count(): Parameter must be an array or an object that implements Countable in /opt/phpdoc/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1275

Returns

\DeviceAtlas\Properties

The found properties or null of no properties found.

getProperty()

getProperty(  $propertyName,   $param = null) : mixed

Get a specific Property from the current client IP found.

Note: if multiple properties are needed for the same IP it is more efficient to call getProperties() once than repeated calls to getProperty().

Parameters

$propertyName
$param

Throws

\DeviceAtlas\Exception\InvalidPropertyNameException

Thrown if the property name does not exist.

Returns

mixed —

The found property or null if no property found.

getPropertyNames()

getPropertyNames() : array

A set of all the possible property names. The Set contains PropertyName objects that each have a name and an associated data type.

Warning: count(): Parameter must be an array or an object that implements Countable in /opt/phpdoc/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1275 Warning: count(): Parameter must be an array or an object that implements Countable in /opt/phpdoc/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1275

Returns

array —

array of possible property names.

getIp()

getIp(  $keyVals) : string

Get the most suitable IP address from the given keyVals set of HTTP headers. This function checks the headers defined in HEADERS_TO_CHECK.

Parameters

$keyVals
Warning: count(): Parameter must be an array or an object that implements Countable in /opt/phpdoc/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1275 Warning: count(): Parameter must be an array or an object that implements Countable in /opt/phpdoc/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1275

Returns

string —

The most suitable IP or NULL if a suitable IP could not be found.

isPublicIp()

isPublicIp(  $ip) : boolean

An IP address is considered public if it is not in any of the following ranges: <pre> 1) any local address IP: 0

2) a local loopback address range: 127/8

3) a site local address i.e. IP is in any of the ranges: range: 10/8 range: 172.16/12 range: 192.168/16

4) a link local address range: 169.254/16

Parameters

$ip
Warning: count(): Parameter must be an array or an object that implements Countable in /opt/phpdoc/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1275 Warning: count(): Parameter must be an array or an object that implements Countable in /opt/phpdoc/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1275

Returns

boolean —

True if it is a public IP, false if the IP is invalid or is not public.

buildDataFile()

buildDataFile(  $dataFileDownloadUrl) 

Parameters

$dataFileDownloadUrl
Warning: count(): Parameter must be an array or an object that implements Countable in /opt/phpdoc/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1275 Warning: count(): Parameter must be an array or an object that implements Countable in /opt/phpdoc/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1275

downloadAndLoadDataFile()

downloadAndLoadDataFile(  $dataFileDownloadUrl) 

Parameters

$dataFileDownloadUrl
Warning: count(): Parameter must be an array or an object that implements Countable in /opt/phpdoc/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1275 Warning: count(): Parameter must be an array or an object that implements Countable in /opt/phpdoc/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1275

getDeviceAtlasApiVersion()

getDeviceAtlasApiVersion() 

Warning: count(): Parameter must be an array or an object that implements Countable in /opt/phpdoc/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1275 Warning: count(): Parameter must be an array or an object that implements Countable in /opt/phpdoc/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1275

downloadAndLoadDataFileFromUrl()

downloadAndLoadDataFileFromUrl(  $dataFile,   $dataFileManager = null) 

Load the DeviceAtlas data file data from a URL.

Schedule a reload if data file is stale. Re-download data file if local data file is corrupted.

Parameters

$dataFile
$dataFileManager

Throws

\DeviceAtlas\Exception\DataLoadingException

when there is a problem downloading or loading the data file.

scheduleReloadAndFinishRequest()

scheduleReloadAndFinishRequest(  $dataFile,   $dataFileManager) 

A secondary register_shutdown_call to make sure the reload method is called as the last one.

Parameters

$dataFile
$dataFileManager
Warning: count(): Parameter must be an array or an object that implements Countable in /opt/phpdoc/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1275 Warning: count(): Parameter must be an array or an object that implements Countable in /opt/phpdoc/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1275

reload()

reload(  $dataFile,   $dataFileManager,   $isScheduled = false) 

Downloads a new data file if the existing one is stale.

Loads data file into API.

Parameters

$dataFile
$dataFileManager
$isScheduled
Warning: count(): Parameter must be an array or an object that implements Countable in /opt/phpdoc/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1275 Warning: count(): Parameter must be an array or an object that implements Countable in /opt/phpdoc/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1275

dataLoaded()

dataLoaded() 

Check if the given propertyName is not null and exists in the data file.

Warning: count(): Parameter must be an array or an object that implements Countable in /opt/phpdoc/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1275 Warning: count(): Parameter must be an array or an object that implements Countable in /opt/phpdoc/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1275

propertyNameExists()

propertyNameExists(  $propertyName) 

Check if the given propertyName is not null and exists in the data file. Calls to this method must be sure that the data object is already loaded.

Parameters

$propertyName

Throws

\DeviceAtlas\Exception\InvalidPropertyNameException

extractIp()

extractIp(  $headerName,   $headerValue) 

Extracts and cleans an IP address from the headerValue. Some headers such as "X-Forwarded-For" can contain multiple IP addresses such as: clientIP, proxy1, proxy2.

..

This method splits up the headerValue and takes the most appropriate value as the IP.

Parameters

$headerName
$headerValue
Warning: count(): Parameter must be an array or an object that implements Countable in /opt/phpdoc/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1275 Warning: count(): Parameter must be an array or an object that implements Countable in /opt/phpdoc/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1275

checkReloadSchedule()

checkReloadSchedule(  $dataFile,   $dataFileManager,   $forceReload = false) 

Parameters

$dataFile
$dataFileManager
$forceReload
Warning: count(): Parameter must be an array or an object that implements Countable in /opt/phpdoc/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1275 Warning: count(): Parameter must be an array or an object that implements Countable in /opt/phpdoc/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1275

callFailureCallback()

callFailureCallback(\Exception  $ex,   $dataFile) 

Call user defined callback if set.

Parameters

\Exception $ex
$dataFile
Warning: count(): Parameter must be an array or an object that implements Countable in /opt/phpdoc/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1275 Warning: count(): Parameter must be an array or an object that implements Countable in /opt/phpdoc/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1275

getIpFromServer()

getIpFromServer() : string

Get the most suitable IP address from the given request. This function checks the headers defined in CarrierApi::HEADERS_TO_CHECK.

Warning: count(): Parameter must be an array or an object that implements Countable in /opt/phpdoc/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1275 Warning: count(): Parameter must be an array or an object that implements Countable in /opt/phpdoc/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1275

Returns

string —

The found IP address or null if no IP found.