\Mobi_Mtld_DA_Carrier_CarrierApi

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
__construct()
loadDataFromFile()
getDataFileCopyright()
getDataFileCreationDate()
getDataFileVersion()
getProperties()
getProperty()
getPropertyNames()
getIp()
isPublicIp()
$HEADERS_TO_CHECK
VERSION
MIN_PHP_VERSION
dataLoaded()
propertyNameExists()
extractIp()
No protected properties found
N/A
isIpSpecial()
$ipSpecial
$data
$MISSING_DATA_EX
$INVALID_PROP_EX
N/A

Constants

VERSION

VERSION = 'DEVICEATLAS_API_VERSION'

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

$ipSpecial

$ipSpecial : 

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(  $path) 

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

Parameters

$path

Throws

\IOException

Thrown when there is a problem loading the file.

\Mobi_Mtld_DA_Exception_DataFileException

Thrown when there is a problem with the data file.

getDataFileCopyright()

getDataFileCopyright() : string

Returns the data file copyright text.

Returns

string —

the copyright

getDataFileCreationDate()

getDataFileCreationDate() : string

Returns the data file creation date in ISO8601 format.

Returns

string —

the creationDate

getDataFileVersion()

getDataFileVersion() : string

Returns the version of the data file.

Returns

string —

the version

getProperties()

getProperties(  $ipv4) : \Mobi_Mtld_DA_Properties

Parameters

$ipv4

Returns

\Mobi_Mtld_DA_Properties

The found properties or null of no properties found.

getProperty()

getProperty(  $ipv4,   $propertyName) : \Mobi_Mtld_DA_Property

Parameters

$ipv4
$propertyName

Throws

\Mobi_Mtld_DA_Exception_InvalidPropertyNameException

Thrown if the property name does not exist.

\Mobi_Mtld_DA_Exception_InvalidPropertyNameException

Thrown if the property name does not exist.

Returns

\Mobi_Mtld_DA_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.

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

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

Returns

boolean —

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

dataLoaded()

dataLoaded() 

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

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

\Mobi_Mtld_DA_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

isIpSpecial()

isIpSpecial(  $ip) 

Check if ip is a special case return true and put the value in the ipSpecial property

Parameters

$ip