-
-
Save nathan-fiscaletti/260491c0f8d7e80ee2bbc27f3a732072 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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