php根据IP获取所在省份-淘宝api接口

这里用的file_put_contents,你也可以用别的,直接怼代码:

//拼接传递的参数
$ip = '175.12.53.12'
$opts = array(
'http'=>array(
'method'=>"GET",
'header'=>"Content-type:application/x-www-form-urlencoded ",
)
);
//获取Ip所属位置
$context = stream_context_create($opts);
$fp = file_get_contents('http://ip.taobao.com/service/getIpInfo.php?ip='.$ip, false, $context);
$addrInfo = json_decode($fp, true);
var_dump($addrInfo);

返回的数据样式为:

array(2) { ["code"]=> int(0) ["data"]=> array(13) { ["ip"]=> string(12) "175.12.53.12" ["country"]=> string(6) "中国" ["area"]=> string(0) "" ["region"]=> string(6) "湖南" ["city"]=> string(6) "长沙" ["county"]=> string(2) "XX" ["isp"]=> string(6) "电信" ["country_id"]=> string(2) "CN" ["area_id"]=> string(0) "" ["region_id"]=> string(6) "430000" ["city_id"]=> string(6) "430100" ["county_id"]=> string(2) "xx" ["isp_id"]=> string(6) "100017" } }
原文地址:https://www.cnblogs.com/wxdblog/p/8891482.html