Skip to content

Instantly share code, notes, and snippets.

@nathan-fiscaletti
Created August 15, 2018 15:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nathan-fiscaletti/260491c0f8d7e80ee2bbc27f3a732072 to your computer and use it in GitHub Desktop.
Save nathan-fiscaletti/260491c0f8d7e80ee2bbc27f3a732072 to your computer and use it in GitHub Desktop.
<?php
use IPStack\PHP\GeoLookup;
$geoLookup = new GeoLookup('acecac3893c90871c3', false, 10);
// 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();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment