Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?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
You can’t perform that action at this time.