Skip to content

Instantly share code, notes, and snippets.

@olivsinz
Created May 23, 2020 12:45
Show Gist options
  • Save olivsinz/41bf1907503b084b56cbbed7394e9829 to your computer and use it in GitHub Desktop.
Save olivsinz/41bf1907503b084b56cbbed7394e9829 to your computer and use it in GitHub Desktop.
Get Your User's Geolocation
<?php
/**
*
*/
trait UsersGeolocation
{
// Get User's Geolocation
function get_timezone()
{
$ip = file_get_contents("http://ipecho.net/plain");
$url = 'http://ip-api.com/json/'.$ip;
$tz = file_get_contents($url);
$tz = json_decode($tz, true)['timezone'];
return $tz;
}
// Get User's Geolocation
function get_user_geolocation()
{
$ip = file_get_contents("http://ipecho.net/plain");
$url = 'http://ip-api.com/json/'.$ip;
$geo = file_get_contents($url);
$geo = [
'country' => json_decode($geo, true)['country'],
'city' => json_decode($geo, true)['city'],
'countryCode' => json_decode($geo, true)['countryCode'],
'lat' => json_decode($geo, true)['lat'],
'long' => json_decode($geo, true)['long'],
];
return $geo;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment