萬盛學電腦網

 萬盛學電腦網 >> 網絡編程 >> php編程 >> php根據新浪淘寶ip 獲取json數據的處理數組的方法

php根據新浪淘寶ip 獲取json數據的處理數組的方法

class="area">

    利用新浪或者淘寶開放api調用ip獲取地理位置的時候返回數據是以json形式展現的。但是怎麼才能 把這些文件成功賦值給某一變量呢?

    \u4e2d\u56fd 這種怎麼進行轉碼呢?PHP Code復制內容到剪貼板
    1. 返回數據如下 {"code":0,"data":{"country":"\u4e2d\u56fd","country_id":"CN","area":"\u534e\u5317","area_id":"100000","region":"\u5317\u4eac\u5e02","region_id":"110000","city":"\u5317\u4eac\u5e02","city_id":"110100","county":"","county_id":"-1","isp":"\u8054\u901a","isp_id":"100026","ip":"123.123.123.123"}}  
    2.   
    3. 但是怎麼才能 把這些文件成功賦值給某一變量呢?  
    4. \u4e2d\u56fd 這種怎麼進行轉碼呢?  
    5. 代碼如下  
    6.   
    7. $ip="123.123.123.123";  
    8. //獲取api的數據病賦值、$res  
    9. $res = @file_get_contents('http://ip.taobao.com/service/getIpInfo.php?ip='.$ip);  
    10.   
    11. //接收的值如下   
    12. //{"code":0,"data":{"country":"\u4e2d\u56fd","country_id":"CN","area":"\u534e\u5317","area_id":"100000","region":"\u5317\u4eac\u5e02","region_id":"110000","city":"\u5317\u4eac\u5e02","city_id":"110100","county":"","county_id":"-1","isp":"\u8054\u901a","isp_id":"100026","ip":"123.123.123.123"}}  
    13.   
    14.   
    15. 進行數據轉換   
    16.   
    17. $json=preg_replace("#\\\u([0-9a-f]{4})#ie", "iconv('UCS-2BE', 'UTF-8', pack('H4', '\\1'))", $res);  
    18.   
    19. //轉換完成以後的代碼如下   
    20.   
    21. {"code":0,"data":{"country":"中國","country_id":"CN","area":"華北","area_id":"100000","region":"北京市","region_i
copyright © 萬盛學電腦網 all rights reserved