|
| 1 | +# IP2Location PHP API |
| 2 | + |
| 3 | +This module allows user to geolocate the country, region, city, coordinates, zip code, ISP, domain name, timezone, connection speed, IDD code, area code, weather station code, weather station name, MCC, MNC, mobile brand name, elevation and usage type that any IP address or host name originates from by calling the **IP2Location API**. |
| 4 | + |
| 5 | + |
| 6 | +## Getting Started |
| 7 | +To begin, an API key is required for this module to function. Find further information at https://www.ip2location.com/web-service |
| 8 | + |
| 9 | + |
| 10 | + |
| 11 | +## Usage |
| 12 | + |
| 13 | +``` |
| 14 | +<?php |
| 15 | +
|
| 16 | +class IP2LocationAPI |
| 17 | +{ |
| 18 | + public $countryCode; |
| 19 | + public $countryName; |
| 20 | + public $regionName; |
| 21 | + public $cityName; |
| 22 | + public $latitude; |
| 23 | + public $longitude; |
| 24 | + public $zipCode; |
| 25 | + public $isp; |
| 26 | + public $domain; |
| 27 | + public $timeZone; |
| 28 | + public $netSpeed; |
| 29 | + public $iddCode; |
| 30 | + public $areaCode; |
| 31 | + public $weatherStationCode; |
| 32 | + public $weatherStationName; |
| 33 | + public $mcc; |
| 34 | + public $mnc; |
| 35 | + public $mobileBrand; |
| 36 | + public $elevation; |
| 37 | + public $usageType; |
| 38 | +
|
| 39 | + protected $apiKey; |
| 40 | + protected $package; |
| 41 | + protected $useSSL; |
| 42 | +
|
| 43 | + public function __construct($apiKey = '', $package = 'WS24', $useSSL = false) |
| 44 | + { |
| 45 | + $this->apiKey = $apiKey; |
| 46 | + $this->package = $package; |
| 47 | + $this->useSSL = $useSSL; |
| 48 | + } |
| 49 | +
|
| 50 | + public function query($ip) |
| 51 | + { |
| 52 | + if (!filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) { |
| 53 | + return false; |
| 54 | + } |
| 55 | +
|
| 56 | + $response = $this->get('http' . (($this->useSSL) ? 's' : '') . '://api.ip2location.com/?' . http_build_query([ |
| 57 | + 'key' => $this->apiKey, |
| 58 | + 'ip' => $ip, |
| 59 | + 'package' => $this->package, |
| 60 | + 'format' => 'json', |
| 61 | + ])); |
| 62 | +
|
| 63 | + if (($json = json_decode($response)) === null) { |
| 64 | + return false; |
| 65 | + } |
| 66 | +
|
| 67 | + if (isset($json->response)) { |
| 68 | + return false; |
| 69 | + } |
| 70 | +
|
| 71 | + $this->countryCode = (string) (isset($json->country_code)) ? $json->country_code :'N/A'; |
| 72 | + $this->countryName = (string) (isset($json->country_name)) ? $json->country_name :'N/A'; |
| 73 | + $this->regionName = (string) (isset($json->region_name)) ? $json->region_name :'N/A'; |
| 74 | + $this->cityName = (string) (isset($json->city_name)) ? $json->city_name :'N/A'; |
| 75 | + $this->latitude = (float) (isset($json->latitude)) ? $json->latitude :'N/A'; |
| 76 | + $this->longitude = (float) (isset($json->longitude)) ? $json->longitude :'N/A'; |
| 77 | + $this->zipCode = (string) (isset($json->zip_code)) ? $json->zip_code :'N/A'; |
| 78 | + $this->timeZone = (string) (isset($json->time_zone)) ? $json->time_zone :'N/A'; |
| 79 | + $this->isp = (string) (isset($json->isp)) ? $json->isp :'N/A'; |
| 80 | + $this->domain = (string) (isset($json->domain)) ? $json->domain :'N/A'; |
| 81 | + $this->netSpeed = (string) (isset($json->net_speed)) ? $json->net_speed :'N/A'; |
| 82 | + $this->iddCode = (string) (isset($json->idd_code)) ? $json->idd_code :'N/A'; |
| 83 | + $this->areaCode = (string) (isset($json->area_code)) ? $json->area_code :'N/A'; |
| 84 | + $this->weatherStationCode = (string) (isset($json->weather_station_code)) ? $json->weather_station_code :'N/A'; |
| 85 | + $this->weatherStationName = (string) (isset($json->weather_station_name)) ? $json->weather_station_name :'N/A'; |
| 86 | + $this->mcc = (string) (isset($json->mcc)) ? $json->mcc :'N/A'; |
| 87 | + $this->mnc = (string) (isset($json->mnc)) ? $json->mnc :'N/A'; |
| 88 | + $this->mobileBrand = (string) (isset($json->mobile_brand)) ? $json->mobile_brand :'N/A'; |
| 89 | + $this->elevation = (int) (isset($json->elevation)) ? $json->elevation :'N/A'; |
| 90 | + $this->usageType = (string) (isset($json->usage_type)) ? $json->usage_type :'N/A'; |
| 91 | +
|
| 92 | + return true; |
| 93 | + } |
| 94 | +
|
| 95 | + private function get($url) |
| 96 | + { |
| 97 | + $ch = curl_init(); |
| 98 | +
|
| 99 | + curl_setopt($ch, CURLOPT_URL, $url); |
| 100 | + curl_setopt($ch, CURLOPT_FAILONERROR, 1); |
| 101 | + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
| 102 | + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); |
| 103 | + curl_setopt($ch, CURLOPT_USERAGENT, 'IP2LocationAPI_PHP-1.0.0'); |
| 104 | + curl_setopt($ch, CURLOPT_TIMEOUT, 3); |
| 105 | + $response = curl_exec($ch); |
| 106 | +
|
| 107 | + return $response; |
| 108 | + } |
| 109 | +} |
| 110 | +
|
| 111 | +?> |
| 112 | +``` |
| 113 | + |
| 114 | + |
0 commit comments