Geocode/ip to geo
Zhangchao2 (讨论 | 贡献) (→XML示例) |
|||
(未显示1个用户的10个中间版本) | |||
第1行: | 第1行: | ||
− | == ''' | + | == '''geocode/ip_to_geo''' == |
根据ip返回地理信息 | 根据ip返回地理信息 | ||
== '''URL''' == | == '''URL''' == | ||
− | http://api.t.sina.com.cn/geocode/ip_to_geo.(json|xml) | + | http://api.t.sina.com.cn/location/geocode/ip_to_geo.(json|xml) |
== '''支持格式''' == | == '''支持格式''' == | ||
第12行: | 第12行: | ||
== '''是否需要登录''' == | == '''是否需要登录''' == | ||
− | ''' | + | '''true''' |
== '''请求数限制''' == | == '''请求数限制''' == | ||
'''true''' | '''true''' | ||
第30行: | 第30行: | ||
| style="text-align:center;" |true | | style="text-align:center;" |true | ||
| style="text-align:center;" |string | | style="text-align:center;" |string | ||
− | | | + | | 需要获取坐标的ip,多个ip使用“,”分隔 |
|} | |} | ||
第38行: | 第38行: | ||
<pre> | <pre> | ||
<?xml version="1.0" encoding="utf-8" ?> | <?xml version="1.0" encoding="utf-8" ?> | ||
− | < | + | <geoResult> |
<geos> | <geos> | ||
<geo> | <geo> | ||
− | |||
<city>0010</city> | <city>0010</city> | ||
<longitude>116.39794</longitude> | <longitude>116.39794</longitude> | ||
第54行: | 第53行: | ||
... | ... | ||
</geos> | </geos> | ||
− | </ | + | </geoResult> |
</pre> | </pre> | ||
=== '''JSON示例''' === | === '''JSON示例''' === | ||
<pre> | <pre> | ||
− | { | + | |
− | + | {"geos": | |
− | { | + | { |
− | + | "city":"0010", | |
− | + | "longitude":"116.39794", | |
− | + | "latitude":"39.90817", | |
− | + | "city_name":"\u5317\u4eac", | |
− | + | "province":32, | |
− | + | "prov_name":"\u5317\u4eac", | |
− | + | "pinyin":"beijing", | |
− | + | "more":"\u4e2d\u56fd\t\u5317\u4eac\t\u5317\u4eac\t\t\u7535\u4fe1\u901a\t\t" | |
− | + | } | |
− | + | ||
} | } | ||
+ | |||
</pre> | </pre> | ||
=== '''字段说明 ''' === | === '''字段说明 ''' === | ||
− | + | :city:ip所属城市代码 | |
− | + | :longitude:ip所在经度 | |
− | + | :latitude:ip所在纬度 | |
− | + | :city_name:ip所在城市 | |
− | + | :prov_name:ip所在省份 | |
− | + | :pinyin:ip所在城市的拼音 | |
− | + | :more:更多ip地址信息 | |
− | + | ||
− | + | ||
− | == ''' | + | == '''其它''' == |
+ | |||
+ | === '''PHP示例''' === | ||
+ | 需要开启PHP的cURL扩展 | ||
<pre> | <pre> | ||
− | + | <?php | |
− | + | //改为你的微博账号,用户名和密码之间用:隔开 | |
+ | $userPwd = 'user@sina.com:pwd'; | ||
+ | //改为你微博应用的appkey | ||
+ | $appkey = '0123456789'; | ||
− | + | $url = "http://api.t.sina.com.cn/location/geocode/ip_to_geo.xml?ip=60.194.172.177&source=$appkey"; | |
− | http://api.t.sina.com.cn/geocode/ip_to_geo. | + | |
+ | $rst = curlSample($url,$userPwd); | ||
+ | |||
+ | echo $rst; | ||
+ | |||
+ | function curlSample($url,$userPwd,$postFields = '',$header = ''){ | ||
+ | $ch = curl_init() or die (curl_error()) ; | ||
+ | curl_setopt($ch,CURLOPT_URL,$url); | ||
+ | curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); | ||
+ | curl_setopt($ch,CURLOPT_TIMEOUT,30); | ||
+ | if(!empty($userPwd)){ | ||
+ | curl_setopt($ch,CURLOPT_USERPWD,$userPwd); | ||
+ | } | ||
+ | if(!empty($postFields)){ | ||
+ | curl_setopt($ch,CURLOPT_POST,true); | ||
+ | curl_setopt($ch,CURLOPT_POSTFIELDS,$postFields); | ||
+ | } | ||
+ | if(!empty($header)){ | ||
+ | curl_setopt($ch, CURLOPT_HTTPHEADER,$header); | ||
+ | } | ||
+ | $result = curl_exec($ch) or die (curl_error($ch)); | ||
+ | curl_close($ch); | ||
+ | return $result; | ||
+ | } | ||
+ | ?> | ||
</pre> | </pre> |
2011年8月11日 (四) 14:28的最后版本
目录 |
geocode/ip_to_geo
根据ip返回地理信息
URL
http://api.t.sina.com.cn/location/geocode/ip_to_geo.(json%7Cxml)
支持格式
XML/JSON
HTTP请求方式
GET
是否需要登录
true
请求数限制
true
请求参数
参数 | 必选 | 类型及范围 | 说明 |
---|---|---|---|
source | true | string | 申请应用时分配的AppKey,调用接口时候代表应用的唯一身份 |
ip | true | string | 需要获取坐标的ip,多个ip使用“,”分隔 |
返回结果
XML示例
<?xml version="1.0" encoding="utf-8" ?> <geoResult> <geos> <geo> <city>0010</city> <longitude>116.39794</longitude> <latitude>39.90817</latitude> <city_name>北京</city_name> <province>32</province> <prov_name>北京</prov_name> <pinyin>beijing</pinyin> <more>中国 北京 北京 电信通</more> <ip>60.194.172.177</ip> </geo> ... </geos> </geoResult>
JSON示例
{"geos": { "city":"0010", "longitude":"116.39794", "latitude":"39.90817", "city_name":"\u5317\u4eac", "province":32, "prov_name":"\u5317\u4eac", "pinyin":"beijing", "more":"\u4e2d\u56fd\t\u5317\u4eac\t\u5317\u4eac\t\t\u7535\u4fe1\u901a\t\t" } }
字段说明
- city:ip所属城市代码
- longitude:ip所在经度
- latitude:ip所在纬度
- city_name:ip所在城市
- prov_name:ip所在省份
- pinyin:ip所在城市的拼音
- more:更多ip地址信息
其它
PHP示例
需要开启PHP的cURL扩展
<?php //改为你的微博账号,用户名和密码之间用:隔开 $userPwd = 'user@sina.com:pwd'; //改为你微博应用的appkey $appkey = '0123456789'; $url = "http://api.t.sina.com.cn/location/geocode/ip_to_geo.xml?ip=60.194.172.177&source=$appkey"; $rst = curlSample($url,$userPwd); echo $rst; function curlSample($url,$userPwd,$postFields = '',$header = ''){ $ch = curl_init() or die (curl_error()) ; curl_setopt($ch,CURLOPT_URL,$url); curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); curl_setopt($ch,CURLOPT_TIMEOUT,30); if(!empty($userPwd)){ curl_setopt($ch,CURLOPT_USERPWD,$userPwd); } if(!empty($postFields)){ curl_setopt($ch,CURLOPT_POST,true); curl_setopt($ch,CURLOPT_POSTFIELDS,$postFields); } if(!empty($header)){ curl_setopt($ch, CURLOPT_HTTPHEADER,$header); } $result = curl_exec($ch) or die (curl_error($ch)); curl_close($ch); return $result; } ?>
文档更新时间: 2011-08-11