0

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.

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Sameed Alam Qureshi
  • 293
  • 1
  • 3
  • 15

3 Answers3

2

Use function file_get_contents.

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

DEMO.

Rikesh
  • 26,156
  • 14
  • 79
  • 87
0

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

Community
  • 1
  • 1
Mihai8
  • 3,113
  • 1
  • 21
  • 31
0

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'];
garbetjie
  • 579
  • 3
  • 10