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.

§ Find location of IP address in PHP

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"
}

§ Find city of IP address in PHP

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

§ Find postal code of IP address in PHP

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

§ Find state / region / region code of IP address in PHP

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

§ Find country / country code of IP address in PHP

$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

§ Find continent code of IP address in PHP

$continent_code = file_get_contents('https://ipapi.co/50.50.50.50/continent_code/');
echo $continent_code;

Output

§ Find latitude / longitude of IP address in PHP

$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

§ Find timezone of IP address in PHP

$timezone = file_get_contents('https://ipapi.co/50.50.50.50/timezone/');
echo $timezone;

Output

§ Find UTC offset of IP address in PHP

$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

§ Find country calling code of IP address in PHP

$country_calling_code = file_get_contents('https://ipapi.co/50.50.50.50/country_calling_code/');
echo $country_calling_code;

Output

§ Find currency of IP address in PHP

$currency = file_get_contents('https://ipapi.co/50.50.50.50/currency/');
echo $currency;

Output

§ Find common languages spoken at the location of an IP address

$languages = file_get_contents('https://ipapi.co/50.50.50.50/languages/');
echo $languages;

Output

§ Find if IP address is in EU / Europe

$in_eu = file_get_contents('https://ipapi.co/50.50.50.50/in_eu/');
echo $in_eu;

Output

§ Find ASN of IP address in PHP

$asn = file_get_contents('https://ipapi.co/50.50.50.50/asn/');
echo $asn;

Output

// ASN : autonomous system number
AS5650

§ Find organization of IP address in PHP

$org = file_get_contents('https://ipapi.co/50.50.50.50/org/');
echo $org;

Output

Frontier Communications of America, Inc.

§ Find city, state and country of IP address in PHP

$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