Skip to content

Instantly share code, notes, and snippets.

@mikelfo
Created April 1, 2017 21:31
Show Gist options
  • Select an option

  • Save mikelfo/3c48589e995c20dc500ae7466c86d511 to your computer and use it in GitHub Desktop.

Select an option

Save mikelfo/3c48589e995c20dc500ae7466c86d511 to your computer and use it in GitHub Desktop.
a simple Symfony service implementing IP geolocation using the free IPInfoDB HTTP API (http://ipinfodb.com/ip_location_api.php)
<?php
/**
* Description of Free GeoIP location service by IPInfoDB --Symfony 2.8--
* @see http://www.ipinfodb.com/ip_location_api.php
* @note The IPInfoDB API supports both the http and https API queries.
* @important Requires a valid IPInfoDB account (free)
*
* @version 1.0.1
*
* @requires Buzz HTTP library. https://github.com/sensiolabs/SensioBuzzBundle
* and cUrl (php5-curl), etc.
*
* @author Mikel Fernández <mikel.fernandez (at) owasys.com>
* @copy (C) 2016 Owasys (https://www.owasys.com)
* @license MIT
*/
namespace MyApp\MyBundleBundle\Services;
use Symfony\Component\DependencyInjection\ContainerInterface as Container;
/**
* IPInfoDB class
* @see IPInfoDB Account: username:mikel.fernandez email:[email protected] pwd: erictel66
*/
class IPInfoDB {
// we use the country resolution endpoint, as it is faster than the city resolution
const COUNTRY_API_URL = "api.ipinfodb.com/v3/ip-country/";
// if better resolution is needed. It works for Ipv4 and Ipv6 addresses
const CITY_API_URL = "api.ipinfodb.com/v3/ip-city/";
private $logger;
private $buzz;
private $apikey;
public function __construct($logger, Container $container, $apikey) {
$this->logger = $logger;
$this->buzz = $container->get('buzz');
$this->apikey = $apikey; // inject config parameter to this service %ipinfodb_apikey%
}
/**
* Gets the geolocation information available on IPInfoDB free API about the IP address passed as argument
* @param string $ip IPv4 address
* @param string $method 'http' or 'https' in lower case are allowed. Default is HTTP
* @return array|null null in case of error, if successful an array with two properties:
* @field name international name of the country e.g. Spain
* @field code ISO3166-alpha2 cuntry code
*/
public function getCountryFromIP($ip, $method = 'http') {
$headers = [];
//$headers = ['Content-Type', 'application/x-www-form-urlencoded'];
$toBeSent = array(
'key' => $this->apikey,
'ip' => $ip,
);
$url = $method . '://' . self::COUNTRY_API_URL . '?' . http_build_query($toBeSent);
$response = $this->buzz->get($url, $headers);
if ($response->getStatusCode() == 200) {
$pieces = explode(";", $response->getContent());
if ($pieces[0] === "OK") {
return array(
'code' => $pieces[3],
'name' => $pieces[4]
);
}
}
return null;
}
/**
* Gets the geolocation information available in IPInfoDB free API about the IP address passed as argument
* It can return results with better resolution than @method getCountryFromIP() up to the city level
* @param string $ip IPv4 address
* @param string $method 'http' or 'https' in lower case are allowed. Default is HTTP
* @return array|null null in case of error, if successful an array with two properties:
* @field name international name of the country e.g. Spain
* @field code ISO3166-alpha2 country code
* @note The result is compatible with that of @method getCountryFromIP() but it *may* also include:
* region, city, postalCode, timezone Offset, latitude and logitude
* Please note that some or all of those fields could be empty
*/
public function getCityFromIP($ip, $method = 'http') {
$toBeSent = array(
'key' => $this->apikey,
'ip' => $ip,
);
$response = $this->buzz->get($method . '://' . self::CITY_API_URL . '?' . http_build_query($toBeSent), []);
if ($response->getStatusCode() == 200) {
$pieces = explode(";", $response->getContent());
if ($pieces[0] === "OK") {
return array(
'code' => $pieces[3],
'name' => $pieces[4],
'region' => $pieces[5],
'city' => $pieces[6],
'postalCode' => $pieces[7],
'lat' => $pieces[8], 'lon' => $pieces[9],
'timezoneOffset' => $pieces[10]
);
}
}
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment