Created
May 23, 2020 12:45
-
-
Save olivsinz/41bf1907503b084b56cbbed7394e9829 to your computer and use it in GitHub Desktop.
Get Your User's Geolocation
This file contains hidden or 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 | |
/** | |
* | |
*/ | |
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