如何在php中使用ip地址获取移动国家代码

时间:2017-12-28 06:41:20

标签: php ip

我必须根据ip获取移动国家代码, 对于印度=> 91,英格兰=> 41等我使用下面的代码,但它不起作用。

代码,

$ip = $_SERVER['REMOTE_ADDR'];
$data = file_get_contents("http://www.geoplugin.net/json.gp?ip=" . $ip);
$data = json_decode($data);

1 个答案:

答案 0 :(得分:0)

提供以下回复

$ip = $_SERVER['REMOTE_ADDR'];
$data = file_get_contents("http://www.geoplugin.net/json.gp?ip=" . $ip);
$data = json_decode($data);
print_($data);

Repsone

{
  ["geoplugin_request"]=>
  string(13) "XX.XX.XX.XXX"
  ["geoplugin_status"]=>
  int(200)
  ["geoplugin_credit"]=>
  string(143) "Some of the returned data includes GeoLite data created by MaxMind, available from <a href='http://www.maxmind.com'>http://www.maxmind.com</a>."
  ["geoplugin_city"]=>
  string(9) "New Delhi"
  ["geoplugin_region"]=>
  string(5) "Delhi"
  ["geoplugin_areaCode"]=>
  string(1) "0"
  ["geoplugin_dmaCode"]=>
  string(1) "0"
  ["geoplugin_countryCode"]=>
  string(2) "IN"
  ["geoplugin_countryName"]=>
  string(5) "India"
  ["geoplugin_continentCode"]=>
  string(2) "AS"
  ["geoplugin_latitude"]=>
  string(4) "28.6"
  ["geoplugin_longitude"]=>
  string(4) "77.2"
  ["geoplugin_regionCode"]=>
  string(2) "07"
  ["geoplugin_regionName"]=>
  string(5) "Delhi"
  ["geoplugin_currencyCode"]=>
  string(3) "INR"
  ["geoplugin_currencySymbol"]=>
  string(7) "&#8360;"
  ["geoplugin_currencySymbol_UTF8"]=>
  string(3) "₨"
  ["geoplugin_currencyConverter"]=>
  float(64.1725)
}

从上面你会得到国家geoplugin_countryCode,它是iso代码,

一旦你有iso国家代码,你就可以轻松获得国家/地区拨号代码。 (保留dailing coutry代码和iso代码映射的数组) 示例

$ country = array('IN'=&gt; 91,......);

我希望这会有所帮助

相关问题