Skip to content

Commit db72b7f

Browse files
committed
Migrated into composer.
1 parent 79ea377 commit db72b7f

7 files changed

Lines changed: 197 additions & 81 deletions

File tree

LICENSE.TXT

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2021 IPInfoDB.com
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,42 +3,44 @@
33
IPInfoDB PHP
44
==============
55

6-
This IPInfoDB PHP class provides a fast lookup of country, region, city, latitude, longitude, ZIP code as values. The IPInfoDB web service is design for server to server call.
6+
This IPInfoDB PHP class provides a fast lookup of country, region, city, latitude, longitude, ZIP code and time zone as values. The IPInfoDB web service is designed for server-to-server call.
77

8-
This sample codes can be used in many types of projects such as:
8+
This library can be used in many types of projects such as:
99

10-
- know where is your web visitors come from
11-
- analyze your web server logs to determine the countries of your visitors
12-
- country blocker
13-
- page redirection according to country
14-
- geotargeting in advertisement
10+
- Locate where is your website visitors come from
11+
- Analyze your web server logs to determine the countries of your visitors
12+
- Country blocker
13+
- Page redirection according to country
14+
- Geotargeting in advertisement
1515

16-
IPInfoDB free API is using IP2Location Lite version. IP2Location Commercial version which provides higher accuracy is available at https://www.ip2location.com/web-service with no restrictions on rate limit.
16+
IPInfoDB free API is using IP2Location LITE version. IP2Location Commercial version provides higher accuracy is available at https://www.ip2location.com/web-service with no rate limit.
1717

1818
## Methods
1919
Below are the methods supported in this class.
2020

2121
|Method Name|Description|
2222
|---|---|
2323
|getCountry|Return country code and country name information of the IP address.|
24-
|getCity|Return city information of the IP address, such as region, city, lattitud, longitude, ZIPCode and timezone.|
24+
|getCity|Return city information of the IP address, such as region, city, latitude, longitude, ZIP Code and time zone.|
2525

2626

2727
## FAQ
2828

29-
__How to I get the API key?__
29+
30+
31+
#### How to I get the API key?
3032

3133
Please register your API key at https://ipinfodb.com/register.
3234

3335

3436

35-
__Is there any restiction for using this web service?__
37+
#### Is there any restriction for using this web service?
3638

37-
There is a rate limit of 2 connections per second.
39+
There is a rate limit of 2 queries per second.
3840

3941

4042

4143
Copyright
4244
=========
4345

44-
Copyright (C) 2018 by IPInfoDB.com, support@ipinfodb.com
46+
Copyright (C) 2021 by IPInfoDB.com, support@ipinfodb.com

class.IPInfoDB.php

Lines changed: 0 additions & 45 deletions
This file was deleted.

composer.json

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"name": "ip2location/ipinfodb-php",
3+
"description": "Library to query geolocation information from IPInfoDB free API service.",
4+
"keywords": ["ip2location", "ip2locationlite", "geolocation", "ip geolocation", "isp", "time zone", "mnc", "mcc", "usage type", "zip code", "area code"],
5+
"homepage": "https://www.ipinfodb.com",
6+
"type": "library",
7+
"license": "MIT",
8+
"authors": [
9+
{
10+
"name": "IPInfoDB",
11+
"email": "support@ipinfodb.com",
12+
"homepage": "https://www.ipinfodb.com"
13+
}
14+
],
15+
"require-dev": {
16+
"phpunit/phpunit": "^9.5"
17+
},
18+
"require": {
19+
"php": ">=7.2",
20+
"ext-curl": "*",
21+
"ext-json": "*"
22+
},
23+
"autoload": {
24+
"psr-4": {
25+
"IPInfoDB\\": "src"
26+
}
27+
}
28+
}

examples/example.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
require 'vendor/autoload.php';
4+
5+
$ipinfodb = new \IPInfoDB\Api('YOUR_API_KEY');
6+
7+
// Get country information only (Faster)
8+
$result = $ipinfodb->getCountry('8.8.8.8');
9+
10+
if ($result) {
11+
echo "getCountry()\n";
12+
echo 'Country Code : ' . $result['countryCode'] . "\n";
13+
echo 'Country Name : ' . $result['countryName'] . "\n\n";
14+
}
15+
16+
// Get city information (Slower)
17+
$result = $ipinfodb->getCity('8.8.8.8');
18+
19+
if ($result) {
20+
echo "getCity()\n";
21+
echo 'Country Code : ' . $result['countryCode'] . "\n";
22+
echo 'Country Name : ' . $result['countryName'] . "\n";
23+
echo 'Region Name : ' . $result['regionName'] . "\n";
24+
echo 'City Name : ' . $result['cityName'] . "\n";
25+
echo 'ZIP Code : ' . $result['zipCode'] . "\n";
26+
echo 'Latitude : ' . $result['latitude'] . "\n";
27+
echo 'Longitude : ' . $result['longitude'] . "\n";
28+
echo 'Time Zone : ' . $result['timeZone'] . "\n\n";
29+
}

iplocation.php

Lines changed: 0 additions & 23 deletions
This file was deleted.

src/Api.php

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
<?php
2+
3+
namespace IPInfoDB;
4+
5+
/**
6+
* IPInfoDB API service.
7+
*/
8+
class Api
9+
{
10+
/**
11+
* IPInfoDB API key.
12+
*
13+
* @var string
14+
*/
15+
protected $apiKey;
16+
17+
/**
18+
* Constructor.
19+
*
20+
* @param string $apiKey a IPInfoDB API key
21+
*
22+
* @throws \Exception
23+
*/
24+
public function __construct($apiKey)
25+
{
26+
if (!preg_match('/^[0-9a-z]{64}$/', $apiKey)) {
27+
throw new \Exception(__CLASS__ . ': Invalid IPInfoDB API key.');
28+
}
29+
30+
$this->apiKey = $apiKey;
31+
}
32+
33+
/**
34+
* Get country information by IP address.
35+
*
36+
* @param string $ip
37+
*
38+
* @return array|false
39+
*/
40+
public function getCountry($ip)
41+
{
42+
$response = $this->get('http://api.ipinfodb.com/v3/ip-country?' . http_build_query([
43+
'key' => $this->apiKey,
44+
'format' => 'json',
45+
'ip' => $ip,
46+
]));
47+
48+
if (($json = json_decode($response, true)) === null) {
49+
return false;
50+
}
51+
52+
return $json;
53+
}
54+
55+
/**
56+
* Get city information by IP address.
57+
*
58+
* @param string $ip
59+
*
60+
* @return array|false
61+
*/
62+
public function getCity($ip)
63+
{
64+
$response = $this->get('http://api.ipinfodb.com/v3/ip-city?' . http_build_query([
65+
'key' => $this->apiKey,
66+
'format' => 'json',
67+
'ip' => $ip,
68+
]));
69+
70+
if (($json = json_decode($response, true)) === null) {
71+
return false;
72+
}
73+
74+
return $json;
75+
}
76+
77+
/**
78+
* Call a remote URL using cUrl GET request.
79+
*
80+
* @param string $url
81+
*
82+
* @return string|null
83+
*/
84+
private function get($url)
85+
{
86+
$ch = curl_init();
87+
curl_setopt($ch, CURLOPT_URL, $url);
88+
curl_setopt($ch, CURLOPT_FAILONERROR, true);
89+
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
90+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
91+
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
92+
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
93+
94+
$response = curl_exec($ch);
95+
96+
if (!curl_errno($ch)) {
97+
curl_close($ch);
98+
99+
return $response;
100+
}
101+
102+
curl_close($ch);
103+
}
104+
}

0 commit comments

Comments
 (0)