Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PHP

get country from ip php

<?php
/**
 * For my company in particular the below HTTP_CF_IPCOUNTRY
 * didn't work and there was need to call an API that was in HTTPS
 * and that didn't use any key to access the country ISO2 info.
 */
$userIp = $_SERVER['REMOTE_ADDR'];
$ipLocationObj = json_decode(file_get_contents(
  "https://api.iplocation.net/?ip={$userIp}"
));
$userCountryISO = $ipLocationObj->country_code2 ?? "";
/** ipLocationObj will be an object like this one:
{
	"ip":"xxx.xxx.xxx.xxx",
    "ip_number":"0123456789",
    "ip_version":4,
    "country_name":"Italy",
    "country_code2":"IT",
    "isp":"Wind Telecomunicazioni S.P.A",
    "response_code":"200",
    "response_message":"OK"
}
*/
Source by api.iplocation.net #
 
PREVIOUS NEXT
Tagged: #country #ip #php
ADD COMMENT
Topic
Name
1+7 =