PHP远程读取淘宝IP API JSON(非循环)

首页 - 技术杂谈 本文发布于2017-2-21 13:05:00


读取Json API地址:http://ip.taobao.com/service/getIpInfo.php?ip=1.180.212.98


得到的代码:
{"code":0,"data":{"country":"\u4e2d\u56fd","country_id":"CN","area":"\u534e\u5317","area_id":"100000","region":"\u5185\u8499\u53e4\u81ea\u6cbb\u533a","region_id":"150000","city":"\u5305\u5934\u5e02","city_id":"150200","county":"","county_id":"-1","isp":"\u7535\u4fe1","isp_id":"100017","ip":"1.180.212.98"}}


解析效果如下:


您所在的IP:1.180.212.98, 来自:中国 CN 包头市 电信


代码如下:

<?php
$ip = $_SERVER['REMOTE_ADDR'];//获取客户端IP
$api = "http://ip.taobao.com/service/getIpInfo.php?ip=".$ip.""; 
$json = file_get_contents($api);//得到原页面代码
$obj = json_decode($json,true);//解析json 
$getcode = $obj['code'];//获取code值
if($getcode=="0"){
echo("<li><h3>您所在的IP:".$ip.", 来自:".$obj['data']['country']." ".$obj['data']['country_id']." ".$obj['data']['city']." ".$obj['data']['isp']."</h3></li>");
}else{
echo("<li><h3>您所在的IP:".$ip."</h3></li>");
}
?>

实例地址参考见:http://www.guabu.com/jishuzatan/phpschool/json_taobaoip.php


首页 | 吉凶大全 | 星座配对 | 塔罗牌

提示:本站不提供技术解答服务