IP Lookup in PHP
A collection of PHP code snippets to get real time information (like location, currency, timezone etc.) for an IP address.
A collection of PHP code snippets to get real time information (like location, currency, timezone etc.) for an IP address.
Get the geolocation (geographic location) of an IP address with an API. The example below uses json
as the output format. To switch to a different format, replace json
in the URL path below with other supported formats like csv
, xml
, yaml
& jsonp
.
$ip = '8.8.8.8';
$loc = file_get_contents('https://ipapi.co/'.$ip.'/json/');
echo $loc;
Output
{
"ip": "8.8.8.8",
"city": "Mountain View",
"region": "California",
"region_code": "CA",
"country": "US",
"country_name": "United States",
"continent_code": "NA",
"in_eu": false,
"postal": "94035",
"latitude": 37.386,
"longitude": -122.0838,
"timezone": "America/Los_Angeles",
"utc_offset": "-0700",
"country_calling_code": "+1",
"currency": "USD",
"languages": "en-US,es-US,haw,fr",
"asn": "AS15169",
"org": "Google LLC"
}
If all you need is the city of an IP address, you can use the city
endpoint as shown in the example below. The output is a text string with the city name.
echo file_get_contents('https://ipapi.co/50.50.50.50/city/');
Output
Similarly, you can find the postal code (zip code) as shown below. The output is a text string indicating the postal code.
echo file_get_contents('https://ipapi.co/50.50.50.50/postal/');
Output
To find the region name (state) or region code, you can use the following example.
$region = file_get_contents('https://ipapi.co/50.50.50.50/region/');
$region_code = file_get_contents('https://ipapi.co/50.50.50.50/region_code/');
echo $region;
echo $region_code;
Output
$country_name = file_get_contents('https://ipapi.co/50.50.50.50/country_name/');
$country = file_get_contents('https://ipapi.co/50.50.50.50/country/');
echo $country_name;
echo $country;
Output
$continent_code = file_get_contents('https://ipapi.co/50.50.50.50/continent_code/');
echo $continent_code;
Output
$latitude = file_get_contents('https://ipapi.co/50.50.50.50/latitude/');
$longitude = file_get_contents('https://ipapi.co/50.50.50.50/longitude/');
$latlong = file_get_contents('https://ipapi.co/50.50.50.50/latlong/');
echo $latitude;
echo $longitude;
echo $latlong;
Output
45.704500
-89.386600
45.704500,-89.386600
$timezone = file_get_contents('https://ipapi.co/50.50.50.50/timezone/');
echo $timezone;
Output
$utc_offset = file_get_contents('https://ipapi.co/50.50.50.50/utc_offset/');
echo $utc_offset;
Output
// automatically adjusts with daylight saving time
-0500
$country_calling_code = file_get_contents('https://ipapi.co/50.50.50.50/country_calling_code/');
echo $country_calling_code;
Output
$currency = file_get_contents('https://ipapi.co/50.50.50.50/currency/');
echo $currency;
Output
$languages = file_get_contents('https://ipapi.co/50.50.50.50/languages/');
echo $languages;
Output
$in_eu = file_get_contents('https://ipapi.co/50.50.50.50/in_eu/');
echo $in_eu;
Output
$asn = file_get_contents('https://ipapi.co/50.50.50.50/asn/');
echo $asn;
Output
// ASN : autonomous system number
AS5650
$org = file_get_contents('https://ipapi.co/50.50.50.50/org/');
echo $org;
Output
Frontier Communications of America, Inc.
$ip = '8.8.8.8';
$loc = file_get_contents('https://ipapi.co/'.$ip.'/json/');
$obj = json_decode($loc);
echo $obj->city.", ".$obj->region.", ".$obj->country_name;
Output
Mountain View, California, United States