Skip to content
Permalink
Browse files

Merge pull request #9 from nathan-fiscaletti/v1.3

V1.3
  • Loading branch information...
nathan-fiscaletti committed Jul 18, 2018
2 parents f56f37a + 1de3d99 commit f4debbe63f99e3e2953c949b87b89f05f9b70c0a
@@ -1,7 +1,7 @@
# FreeGeoIp for PHP
> **FreeGeoIp for PHP** is a simple library used to interface with a freegeoip API.
# IPStack for PHP (Geo Location Library)
> **IPStack for PHP** is a simple library used to interface with an IPStack Geo API.
>
> **Hint**: FreeGeoIp for PHP is available through [Composer](https://getcomposer.org). `composer require nafisc/freegeoip-php`.
> **Hint**: IPStack for PHP is available through [Composer](https://getcomposer.org). `composer require nafisc/ipstackgeo-php`.
[![StyleCI](https://styleci.io/repos/115560334/shield?style=flat)](https://styleci.io/repos/115560334)
[![TravisCI](https://travis-ci.org/nathan-fiscaletti/freegeoip-php.svg?branch=master)](https://travis-ci.org/nathan-fiscaletti/freegeoip-php)
@@ -10,36 +10,44 @@
[![Latest Unstable Version](https://poser.pugx.org/nafisc/freegeoip-php/v/unstable?format=flat)](https://packagist.org/packages/nafisc/freegeoip-php)
[![License](https://poser.pugx.org/nafisc/freegeoip-php/license?format=flat)](https://packagist.org/packages/nafisc/freegeoip-php)

Learn more about FreeGeoIP here: [freegeoip.net](http://freegeoip.net)
Learn more about IPStack here: [ipstack.net](https://ipstack.com/product)

### Features
* Retrieve the Geo Location data for any IP address.
* Link to a custom freegeoip server
* Link to a custom ipstack server

### Example Usage

#### Include the `FreeGeoIp` class in your code.
#### Include the `GeoLookup` class in your code.
```php
use FreeGeoIp\PHP\FreeGeoIp;
use IPStack\PHP\GeoLookup;
```

#### Create the FreeGeoIp object
#### Create the GeoLookup object

This will default to `freegeoip.net`.
> Note: From the freegeoip.net web page:
> You're allowed up to 15,000 queries per hour by default. Once this limit is reached, all of your requests will result in HTTP 403, forbidden, until your quota is cleared.
>
>The freegeoip web server is free and open source so if the public service limit is a problem for you, [download it](https://github.com/fiorix/freegeoip/releases/) and run your own instance.
```php
$freeGeoIp = new FreeGeoIp();
// Initialize with your IPStack API Key.
$geoLookup = new GeoLookup(
'acecac3893c90871c3', // API Key
false, // Use HTTPS (IPStack Basic plan and up only, defaults to false)
10 // Timeout in seconds (defaults to 10 seconds)
);
```

Alternately, you can pass it a custom server configuration if you are using the freegeoip binary.
#### Using the the Legacy [FreeGeoIP Binary](https://github.com/fiorix/freegeoip/releases/)

You can still use the legacy FreeGeoIP Binary hosted on a server
> Note: [FreeGeoIP has been deprecated](https://github.com/apilayer/freegeoip/#freegeoip---important-announcement).
```php
use IPStack\Legacy\FreeGeoIp as GeoLookup;
// Address, Port, Protocol, Timeout
$freeGeoIp = new FreeGeoIp(
'freegeoip.net', 80,
'http', 10
$geoLookup = new GeoLookup(
'localhost', // Address hosting the legacy FreeGeoIP Binary
8080, // Port that the binary is running on (defaults to 8080)
'http', // Protocol to use (defaults to http)
10 // Timeout (defaults to 10 seconds)
);
```

@@ -59,11 +67,11 @@ try {
//
// This function will work with hostnames
// or IP addresses.
$location = $freeGeoIp->getLocationFor('github.com');
$location = $geoLookup->getLocationFor('github.com');
// You can alternately look up the information
// for the current client's IP address.
$location = $freeGeoIp->getClientLocation();
$location = $geoLookup->getClientLocation();
// If we are unable to retrieve the location information
// for an IP address, null will be returned.
@@ -1,5 +1,5 @@
{
"name": "nafisc/freegeoip-php",
"name": "nafisc/ipstackgeo-php",
"description": "A PHP library for interfacing with freegeoip APIs",
"type": "library",
"license": "MIT",
@@ -11,12 +11,12 @@
],
"autoload": {
"psr-4": {
"FreeGeoIp\\PHP\\": "src/FreeGeoIp"
"IPStack\\PHP\\": "src/IPStack"
}
},
"autoload-dev": {
"psr-4": {
"FreeGeoIp\\PHP\\Tests\\": "tests/"
"IPStack\\PHP\\Tests\\": "tests/"
}
},
"require": {

This file was deleted.

@@ -0,0 +1,109 @@
<?php
namespace IPStack\PHP;
use GuzzleHttp\Client;
use TixAstronauta\AccIp\AccIp;
/**
* A PHP class for querying a FreeGeoIP server
* for the Geo Location information of an IP.
*/
class GeoLookup
{
/**
* The timeout for the current server.
*
* @var int
*/
private $timeout;
/**
* The API key used to connect to the IPStack API.
*
* @var string
*/
private $api_key;
/**
* If set to true, HTTPS will be used for the connection.
*
* @var bool
*/
private $use_https;
/**
* Construct the FreeGeoIp object with server information.
* Defaults to freegeoip.net.
*
* @param string $api_key
* @param string $server_address
* @param int $server_port
* @param string $server_protocol
* @param int $timeout
*/
public function __construct(
string $api_key,
bool $use_https = false,
int $timeout = 10
) {
$this->timeout = $timeout;
$this->api_key = $api_key;
$this->use_https = $use_https;
}
/**
* Retrieve a location for a specific IP address.
*
* @param string $ip
*
* @return \FreeGeoIp\PHP\Location|null
* @throws \Exception
*/
public function getLocationFor(string $ip)
{
$ret = null;
try {
$response = (new Client([
'base_uri' => (
($this->use_https)
? 'https'
: 'http'
).'://api.ipstack.com/',
'timeout' => $this->timeout,
]))->get($ip.'?access_key='.$this->api_key.'&output=json');
if ($response->getStatusCode() == 200) {
$compiled = json_decode($response->getBody()->getContents(), true);
if (array_key_exists('error', $compiled)) {
throw new \Exception('Error: '.$compiled['error']['info']);
}
$ret = new Location($compiled);
}
} catch (\Exception $e) {
throw $e;
}
return $ret;
}
/**
* Returns a location for the current clients IP address.
*
* @return \FreeGeoIp\PHP\Location
* @throws \Exception
*/
public function getClientLocation()
{
$accIp = new AccIp();
$ip = $accIp->getIpAddress();
if ($ip === false) {
throw new \Exception('Error: Unable to find client IP address.');
}
return $this->getLocationFor($ip);
}
}
@@ -1,8 +1,9 @@
<?php
namespace FreeGeoIp\PHP;
namespace IPStack\PHP\Legacy;
use GuzzleHttp\Client;
use IPStack\PHP\Location;
use TixAstronauta\AccIp\AccIp;
/**
@@ -35,8 +36,8 @@ class FreeGeoIp
* @param int $timeout
*/
public function __construct(
string $server_address = 'freegeoip.net',
int $server_port = 80,
string $server_address,
int $server_port = 8080,
string $server_protocol = 'http',
int $timeout = 10
) {
@@ -56,13 +57,11 @@ public function __construct(
public function getLocationFor(string $ip)
{
$ret = null;
try {
$response = (new Client([
'base_uri' => $this->server_url,
'timeout' => $this->timeout,
]))->get('json/'.$ip);
if ($response->getStatusCode() == 200) {
$ret = new Location(json_decode(
$response->getBody()->getContents(), true));
@@ -84,7 +83,6 @@ public function getClientLocation()
{
$accIp = new AccIp();
$ip = $accIp->getIpAddress();
if ($ip === false) {
throw new \Exception('Unable to find client IP address.');
}
@@ -0,0 +1,49 @@
<?php
namespace IPStack\PHP;
use ExtendedArrays\ReadOnlyRestrictedAssociativeArray as ReadOnlyResAssocArr;
class Location extends ReadOnlyResAssocArr
{
/**
* Fillable properties of the object.
*
* @var array
*/
protected $fillable = [
// These properties are available on all
// IPStack plans.
'ip',
'type',
'continent_code',
'continent_name',
'country_code',
'country_name',
'region_code',
'region_name',
'city',
'zip',
'latitude',
'longitude',
'location',
// These properties require an API
// key with IPStack's Basic plan or higher.
// Free plans will not have access to them.
'time_zone',
'connection',
'currency',
// This property requires an API
// key with IPStack's Enterprise plan
// Other API keys will not have access to it.
'security',
// These properties are used when you are
// using the legacy FreeGeoIp binary.
'zip_code',
'metro_code',
];
}

This file was deleted.

@@ -0,0 +1,18 @@
<?php
namespace IPStack\PHP\Tests;
use PHPUnit\Framework\TestCase;
use IPStack\PHP\GeoLookup;
/**
* @covers \IPStack\PHP\GeoLookup
*/
final class GeoLookupTest extends TestCase {
public function testGetLocationForReturnsLocationObject()
{
$geo = new GeoLookup('d0164200acfaa5ad0a154d1a7398bc90');
$location = $geo->getLocationFor('github.com');
$this->assertInstanceOf(\IPStack\PHP\Location::class, $location);
}
}
@@ -3,10 +3,10 @@
namespace FreeGeoIp\PHP\Tests;
use PHPUnit\Framework\TestCase;
use FreeGeoIp\PHP\Location;
use IPStack\PHP\Location;
/**
* @covers \FreeGeoIp\PHP\Location
* @covers \IPStack\PHP\Location
*/
final class LocationTest extends TestCase {
public function testIsReadOnly()

0 comments on commit f4debbe

Please sign in to comment.
You can’t perform that action at this time.