Skip to content

Commit

Permalink
Add IP2Location web service
Browse files Browse the repository at this point in the history
  • Loading branch information
ip2location committed Sep 18, 2020
1 parent 9bee29d commit 2d44f33
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 10 deletions.
2 changes: 1 addition & 1 deletion LICENSE.TXT
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2017 IP2Location.com
Copyright (c) 2020 IP2Location.com

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
39 changes: 32 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
[![Latest Stable Version](https://img.shields.io/packagist/v/ip2location/ip2location-cakephp.svg)](https://packagist.org/packages/ip2location/ip2location-cakephp)
[![Total Downloads](https://img.shields.io/packagist/dt/ip2location/ip2location-cakephp.svg?style=flat-square)](https://packagist.org/packages/ip2location/ip2location-cakephp)

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


## INSTALLATION
Expand Down Expand Up @@ -41,6 +41,21 @@ namespace App\Controller;
use App\Controller\AppController;
use IP2LocationCakePHP\Controller\IP2LocationCoresController;
// (required) Define IP2Location API key.
define('IP2LOCATION_API_KEY', 'your_api_key');
// (required) Define IP2Location Web service package of different granularity of return information.
define('IP2LOCATION_PACKAGE', 'WS1');
// (optional) Define to use https or http.
define('IP2LOCATION_USESSL', false);
// (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.
define('IP2LOCATION_ADDONS', []);
// (optional) Define Translation information. Refer to https://www.ip2location.com/web-service/ip2location for available languages.
define('IP2LOCATION_LANGUAGE', 'en');
/**
* Tests Controller
*/
Expand All @@ -55,8 +70,9 @@ class TestsController extends AppController
public function index()
{
$IP2Location = new IP2LocationCoresController();
$record = $IP2Location->get('8.8.8.8');
$record = $IP2Location->get('8.8.8.8');
echo 'Result from BIN Database:<br>';
echo 'IP Address: ' . $record['ipAddress'] . '<br>';
echo 'IP Number: ' . $record['ipNumber'] . '<br>';
echo 'ISO Country Code: ' . $record['countryCode'] . '<br>';
Expand All @@ -79,23 +95,32 @@ class TestsController extends AppController
echo 'Mobile Carrier Name: ' . $record['mobileCarrierName'] . '<br>';
echo 'Elevation: ' . $record['elevation'] . '<br>';
echo 'Usage Type: ' . $record['usageType'] . '<br>';
$record = $IP2Location->getWebService('8.8.8.8');
echo 'Result from Web service:<br>';
echo '<pre>';
print_r ($record);
echo '</pre>';
}
}
```
5. Enter the URL <your domain>/Tests and run. You should see the information of **8.8.8.8** IP address.


## DEPENDENCIES
This library requires IP2Location BIN data file or IP2Location API key to function. You may download the BIN data file at
* IP2Location LITE BIN Data (Free): https://lite.ip2location.com
* IP2Location Commercial BIN Data (Comprehensive): https://www.ip2location.com

You can also sign up for [IP2Location Web Service](https://www.ip2location.com/web-service/ip2location) to get one free API key.


## IPv4 BIN vs IPv6 BIN
Use the IPv4 BIN file if you just need to query IPv4 addresses.

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

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


## SUPPORT
Email: support@ip2location.com
Expand Down
37 changes: 35 additions & 2 deletions src/Controller/IP2LocationCoresController.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,27 @@
<?php
namespace IP2LocationCakePHP\Controller;

// Web Service Settings
if(!defined('IP2LOCATION_API_KEY')) {
define('IP2LOCATION_API_KEY', 'demo');
}

if(!defined('IP2LOCATION_PACKAGE')) {
define('IP2LOCATION_PACKAGE', 'WS1');
}

if(!defined('IP2LOCATION_USESSL')) {
define('IP2LOCATION_USESSL', false);
}

if(!defined('IP2LOCATION_ADDONS')) {
define('IP2LOCATION_ADDONS', []);
}

if(!defined('IP2LOCATION_LANGUAGE')) {
define('IP2LOCATION_LANGUAGE', 'en');
}

/**
* IP2LocationCores Controller
*/
Expand All @@ -14,14 +35,13 @@ class IP2LocationCoresController
*/
public function index()
{
//
//
}

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


try {
$records = $obj->lookup($ip, \IP2Location\Database::ALL);
} catch (Exception $e) {
Expand All @@ -30,4 +50,17 @@ public function get($ip, $query = array())
return $records;
}

public function getWebService($ip)
{
$ws = new \IP2Location\WebService(IP2LOCATION_API_KEY, IP2LOCATION_PACKAGE, IP2LOCATION_USESSL);

try {
$records = $ws->lookup($ip, IP2LOCATION_ADDONS, IP2LOCATION_LANGUAGE);
} catch (Exception $e) {
return null;
}

return $records;
}

}

0 comments on commit 2d44f33

Please sign in to comment.