GithubHelp home page GithubHelp logo

xblue-light / ipstackgeo-php Goto Github PK

View Code? Open in Web Editor NEW

This project forked from nathan-fiscaletti/ipstackgeo-php

0.0 1.0 0.0 35 KB

๐Ÿ“ A PHP library for interfacing with IPStack Geo API

License: MIT License

PHP 100.00%

ipstackgeo-php's Introduction

IPStack for PHP (Geo Location Library)

IPStack for PHP is a simple library used to interface with an IPStack Geo API.

Hint: IPStack for PHP is available through Composer. composer require nafisc/ipstackgeo-php.

Maintainability StyleCI TravisCI Latest Stable Version Total Downloads Latest Unstable Version License

Learn more about IPStack here: ipstack.net

Features

  • Retrieve the Geo Location data for any IP address.
  • (Legacy) Link to a custom FreeGeoIP server

Example Usage

Note: See Location.php for a list of available properties on the Location object.

Create the GeoLookup object

use IPStack\PHP\GeoLookup;

$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)
);

Lookup a location for an IP Address

Note: Locations are returned using a library called ExtendedArrays. This library gives us more options on how we access the properties of the location. See the Acessing Array Elements portion of the ExtendedArrays documentation for more information on this.

// Lookup a location for an IP Address
// and catch any exceptions that might
// be thrown by Guzzle or IPStack.
try {
    // Retrieve the location information for 
    // github.com by using it's hostname.
    // 
    // This function will work with hostnames
    // or IP addresses.
    $location = $geoLookup->getLocationFor('github.com');

    // You can alternately look up the information
    // for the current client's IP address.
    $location = $geoLookup->getClientLocation();

    // If we are unable to retrieve the location information
    // for an IP address, null will be returned.
    if ($location == null) {
        echo 'Failed to find location.'.PHP_EOL;
    } else {
        // Convert the location to a standard PHP array.
        print_r($location->_asStdArray());

        // Any of these formats will work for 
        // retrieving a property.
        echo $location->latitude . PHP_EOL;
        echo $location['longitude'] . PHP_EOL;
        echo $location->region_name() . PHP_EOL;
    }
} catch (\Exception $e) {
    echo $e->getMessage();
}

Using the the Legacy FreeGeoIP Binary

You can still use the legacy FreeGeoIP Binary hosted on a server

Note: FreeGeoIP has been deprecated.

use IPStack\PHP\Legacy\FreeGeoIp as GeoLookup;

// Address, Port, Protocol, Timeout
$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)
);

ipstackgeo-php's People

Contributors

peter279k avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.