Code snippet ?
-
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.
-
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"; ?> -
@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"; ?>
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register Login