Sameed Alam Qureshi
Sameed Alam Qureshi

Reputation: 313

Find IP to Country Code in php code

hi i want get country code data from ip address this site give free services like this link for example http://freegeoip.net/json/23.94.30.210 this link show all data but i need only "country_code":"US" from it.

Please tell me how to implement this link in php get only "country_code":"US" from this.

Upvotes: 0

Views: 1783

Answers (3)

garbetjie
garbetjie

Reputation: 589

It looks like the data has been JSON encoded. You will need to use the JSON functions available in PHP to decode this, and retrieve the country code (assuming you've already managed to retrieve the information using cURL).

Here is a sample bit of code:

// $data contains the information shown at http://freegeoip.net/json/23.94.30.210.
$decoded = json_decode($data);
$country_code = $decoded['country_code'];

Upvotes: 0

Mihai8
Mihai8

Reputation: 3147

First, see related question Getting visitors country from their IP. You can use Geo IP Location.

Upvotes: 0

Rikesh
Rikesh

Reputation: 26421

Use function file_get_contents.

$jsonData = file_get_contents("http://freegeoip.net/json/23.94.30.210");
$countryInfo = json_decode($jsonData,true);

DEMO.

Upvotes: 2

Related Questions