GithubHelp home page GithubHelp logo

xblue-light / ipstackapi-php Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 52 KB

A simple PHP wrapper to get a visitors location using IPStack Geo API.

PHP 100.00%
php7 composer guzzle-php-library curlphp ipstack geolocation

ipstackapi-php's Introduction

IPStack Geo API - PHP

A simple PHP wrapper leveraging Guzzle which attempts to determine a visitors geographical location.

Install project dependencies:

$ composer install

Dont have Composer installed? Learn how to install it here: getcomposer.org

Preview demo

Learn more about IPStack here: ipstack.com

Basic usage:

$ipstackAPIClient = new IpstackAPIClient(
    'API_KEY_GOES_HERE', // API Key
    false, // Use HTTPS (IPStack Basic plan and up only, defaults to false)
    10 // Timeout in seconds (defaults to 10 seconds)
);

// Call the getClientLocation() method which returns an array with the visitor location/ip details.
$response = $ipstackAPIClient->getClientLocation();

// Dump the response variable
var_dump($response);

Alternative usage:

$api_key = "API_KEY_GOES_HERE";

try {
    
    $ipstackAPIClient = new IpstackAPIClient($api_key, false, 10);
    $location = $ipstackAPIClient->getClientLocation();
    
    // Error checking to determine if the response array has certain key values set.
    // I noticed if a visitor is using a proxy of some sort these key values can be often unset/null.
    if ($response === null || $response['country_name'] === null || $response['country_code'] === null) {
        echo "Failed to find a location";
    } else {
        // Dump location variable
        var_dump($location);
    }
}
catch (\Exception $e) {
    echo $e->getMessage();
}

Limit rate usage:

try {

    // This will limit the rate of a global resource to 5 requests per minute for all requests.
    $storage = new FileStorage(__DIR__ . "/api.bucket");
    $rate    = new Rate(5, Rate::MINUTE);
    $bucket  = new TokenBucket(5, $rate, $storage);
    $bucket->bootstrap(0);

    // The method consume() will either return true if the tokens were consumed or false with 429 status.
    if (!$bucket->consume(1, $seconds)) {
        http_response_code(429);
        header(sprintf("Retry-After: %d", floor($seconds)));
        exit("Limit reached! Retry after: " . floor($seconds));
    }

    $ipstackAPIClient = new IpstackAPIClient($api_key, false, 10);
    $response = $ipstackAPIClient->getClientLocation();

    if ($response === null || $response['country_name'] === null || $response['country_code'] === null) {
        exit("Failed to find exact location.");
    } 
    else {
        var_dump($response);
        echo $response['country_code'];
        echo "<br/>";
        echo $response['country_name'];
        echo "<br/>";
        echo $response['location']['country_flag'];
    }
}
catch (\Exception $e) {
    echo $e->getMessage();
}

Inspired by and credits also go to: ipstackgeo-php, bandwith-throttle, ipstack.com, phpdotenv

ipstackapi-php's People

Contributors

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