Skip to content

Commit 2d44f33

Browse files
committed
Add IP2Location web service
1 parent 9bee29d commit 2d44f33

File tree

3 files changed

+68
-10
lines changed

3 files changed

+68
-10
lines changed

LICENSE.TXT

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2017 IP2Location.com
3+
Copyright (c) 2020 IP2Location.com
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
[![Latest Stable Version](https://img.shields.io/packagist/v/ip2location/ip2location-cakephp.svg)](https://packagist.org/packages/ip2location/ip2location-cakephp)
33
[![Total Downloads](https://img.shields.io/packagist/dt/ip2location/ip2location-cakephp.svg?style=flat-square)](https://packagist.org/packages/ip2location/ip2location-cakephp)
44

5-
IP2Location CakePHP plugin enables the user to find the country, region, city, coordinates, zip code, time zone, ISP, domain name, connection type, area code, weather, MCC, MNC, mobile brand name, elevation and usage type that any IP address or hostname originates from. It has been optimized for speed and memory utilization. Developers can use the API to query all IP2Location BIN databases for applications written using CakePHP.
5+
IP2Location CakePHP plugin enables the user to find the country, region, city, coordinates, zip code, time zone, ISP, domain name, connection type, area code, weather, MCC, MNC, mobile brand name, elevation and usage type that any IP address or hostname originates from. It has been optimized for speed and memory utilization. Developers can use the API to query all IP2Location BIN databases or web service for applications written using CakePHP.
66

77

88
## INSTALLATION
@@ -41,6 +41,21 @@ namespace App\Controller;
4141
use App\Controller\AppController;
4242
use IP2LocationCakePHP\Controller\IP2LocationCoresController;
4343
44+
// (required) Define IP2Location API key.
45+
define('IP2LOCATION_API_KEY', 'your_api_key');
46+
47+
// (required) Define IP2Location Web service package of different granularity of return information.
48+
define('IP2LOCATION_PACKAGE', 'WS1');
49+
50+
// (optional) Define to use https or http.
51+
define('IP2LOCATION_USESSL', false);
52+
53+
// (optional) Define extra information in addition to the above-selected package. Refer to https://www.ip2location.com/web-service/ip2location for the list of available addons.
54+
define('IP2LOCATION_ADDONS', []);
55+
56+
// (optional) Define Translation information. Refer to https://www.ip2location.com/web-service/ip2location for available languages.
57+
define('IP2LOCATION_LANGUAGE', 'en');
58+
4459
/**
4560
* Tests Controller
4661
*/
@@ -55,8 +70,9 @@ class TestsController extends AppController
5570
public function index()
5671
{
5772
$IP2Location = new IP2LocationCoresController();
58-
$record = $IP2Location->get('8.8.8.8');
5973
74+
$record = $IP2Location->get('8.8.8.8');
75+
echo 'Result from BIN Database:<br>';
6076
echo 'IP Address: ' . $record['ipAddress'] . '<br>';
6177
echo 'IP Number: ' . $record['ipNumber'] . '<br>';
6278
echo 'ISO Country Code: ' . $record['countryCode'] . '<br>';
@@ -79,23 +95,32 @@ class TestsController extends AppController
7995
echo 'Mobile Carrier Name: ' . $record['mobileCarrierName'] . '<br>';
8096
echo 'Elevation: ' . $record['elevation'] . '<br>';
8197
echo 'Usage Type: ' . $record['usageType'] . '<br>';
98+
99+
$record = $IP2Location->getWebService('8.8.8.8');
100+
echo 'Result from Web service:<br>';
101+
echo '<pre>';
102+
print_r ($record);
103+
echo '</pre>';
82104
}
83105
84106
}
85107
```
86108
5. Enter the URL <your domain>/Tests and run. You should see the information of **8.8.8.8** IP address.
87109

88110

111+
## DEPENDENCIES
112+
This library requires IP2Location BIN data file or IP2Location API key to function. You may download the BIN data file at
113+
* IP2Location LITE BIN Data (Free): https://lite.ip2location.com
114+
* IP2Location Commercial BIN Data (Comprehensive): https://www.ip2location.com
115+
116+
You can also sign up for [IP2Location Web Service](https://www.ip2location.com/web-service/ip2location) to get one free API key.
117+
118+
89119
## IPv4 BIN vs IPv6 BIN
90120
Use the IPv4 BIN file if you just need to query IPv4 addresses.
91121

92122
Use the IPv6 BIN file if you need to query BOTH IPv4 and IPv6 addresses.
93123

94-
## DEPENDENCIES (IP2LOCATION BIN DATA FILE)
95-
This library requires IP2Location BIN data file to function. You may download the BIN data file at
96-
* IP2Location LITE BIN Data (Free): https://lite.ip2location.com
97-
* IP2Location Commercial BIN Data (Comprehensive): https://www.ip2location.com
98-
99124

100125
## SUPPORT
101126

src/Controller/IP2LocationCoresController.php

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,27 @@
11
<?php
22
namespace IP2LocationCakePHP\Controller;
33

4+
// Web Service Settings
5+
if(!defined('IP2LOCATION_API_KEY')) {
6+
define('IP2LOCATION_API_KEY', 'demo');
7+
}
8+
9+
if(!defined('IP2LOCATION_PACKAGE')) {
10+
define('IP2LOCATION_PACKAGE', 'WS1');
11+
}
12+
13+
if(!defined('IP2LOCATION_USESSL')) {
14+
define('IP2LOCATION_USESSL', false);
15+
}
16+
17+
if(!defined('IP2LOCATION_ADDONS')) {
18+
define('IP2LOCATION_ADDONS', []);
19+
}
20+
21+
if(!defined('IP2LOCATION_LANGUAGE')) {
22+
define('IP2LOCATION_LANGUAGE', 'en');
23+
}
24+
425
/**
526
* IP2LocationCores Controller
627
*/
@@ -14,14 +35,13 @@ class IP2LocationCoresController
1435
*/
1536
public function index()
1637
{
17-
//
38+
//
1839
}
1940

2041
public function get($ip, $query = array())
2142
{
2243
$obj = new \IP2Location\Database(ROOT . DS . 'vendor' . DS . 'ip2location' . DS . 'ip2location-cakephp' . DS . 'src' . DS . 'Data' . DS . 'IP2LOCATION.BIN', \IP2Location\Database::FILE_IO);
2344

24-
2545
try {
2646
$records = $obj->lookup($ip, \IP2Location\Database::ALL);
2747
} catch (Exception $e) {
@@ -30,4 +50,17 @@ public function get($ip, $query = array())
3050
return $records;
3151
}
3252

53+
public function getWebService($ip)
54+
{
55+
$ws = new \IP2Location\WebService(IP2LOCATION_API_KEY, IP2LOCATION_PACKAGE, IP2LOCATION_USESSL);
56+
57+
try {
58+
$records = $ws->lookup($ip, IP2LOCATION_ADDONS, IP2LOCATION_LANGUAGE);
59+
} catch (Exception $e) {
60+
return null;
61+
}
62+
63+
return $records;
64+
}
65+
3366
}

0 commit comments

Comments
 (0)