Code snippet ?
Solved
GeoIP Service
-
GeoIP is very interesting to run some checks on visitor location (to deliver customised content).
Just wondering if there is a code snippet to extract the country from the results returned.
-
@timconsidine Since the output from the api is just json encoded you can take the output in a variable and parse it:
this example is PHP of course:
<?php //google.com IP for mid-west US: $ip = "142.251.32.14"; //Get string from API URL then convert the json output to PHP obj $geoip = file_get_contents("https://geoip.example.com/json?ip=$ip"); $geoip = json_decode($geoip, true); //Output Country in string: echo "The registered country of the IP $ip is ".$geoip['registered_country']['names']['en']."\r\n"; echo "Also, you can grab the country name via ['country']['names']['en']: ".$geoip['country']['names']['en']."\r\n"; ?>