\DeviceAtlas\CarrierCarrierApi

The main class for the Carrier Identification API. Used to load the data file and to perform lookups using IPv4 addresses. For usage in a Web App it is recommended to use the CarrierApiWeb (CarrierApiWeb.php) class so you don't have to pass the client's IP address manually.

Please note that it is advisable to load only a single instance of this class to avoid multiple loadings of the data file.

Example usage:

    $ip = '62.40.34.220';
    $carrierApi = new CarrierApi();
    $carrierApi->loadDataFromFile('/path/to/sample.dat');

    // get all properties
    $props = $carrierApi->getProperties($ip);
    // .... use the properties ....
    if ($props->containsKey('networkOperator')) {
        $property = $props->get('networkOperator');
        $operatorName = $property->asString();
        print('networkOperator: ' . $operatorName);
    }

    // get a single property
    $mccProp = $carrierApi->getProperty($ip, 'mcc');
    if ($mccProp !== null) {
        $mcc = $mccProp->asString();
        print('MCC: ' . $mcc);
    }

Please see the code in the Examples directory for additional samples.

Summary

Methods
Properties
Constants
buildDataFile()
loadDataFromFile()
downloadAndLoadDataFileFromUrl()
scheduleReloadAndFinishRequest()
reload()
__construct()
getDataFileCopyright()
getDataFileCreationDate()
getDataFileVersion()
getProperties()
getProperty()
getPropertyNames()
getIp()
isPublicIp()
downloadAndLoadDataFile()
getDeviceAtlasApiVersion()
$HEADERS_TO_CHECK
API_VERSION
MIN_PHP_VERSION
dataLoaded()
propertyNameExists()
extractIp()
No protected properties found
N/A
checkReloadSchedule()
callFailureCallback()
$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

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

loadDataFromFile()

loadDataFromFile(  $path) 

Load the data file from the provided path. The data file is reloaded every time this method is called.

Parameters

$path

Throws

\DeviceAtlas\Exception\DataLoadingException

Thrown when there is a problem with the data file.

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

__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

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(  $ipv4) : \DeviceAtlas\Properties

Parameters

$ipv4
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(  $ipv4,   $propertyName) : \DeviceAtlas\Property

Parameters

$ipv4
$propertyName

Throws

\DeviceAtlas\Exception\InvalidPropertyNameException

Thrown if the property name does not exist.

\DeviceAtlas\Exception\InvalidPropertyNameException

Thrown if the property name does not exist.

Returns

\DeviceAtlas\Property

The 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.

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

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