drtj40036
drtj40036
2014-04-05 15:55

在PHP中使用json响应

已采纳

I am trying to use one object from the many provided in my json request.

Trying to obtain only the country name from the data that is given.

$location = file_get_contents('http://freegeoip.net/json/'.$_SERVER['REMOTE_ADDR']);
 echo $location;

The above code gives me the following string:

{"ip":"x.xx.xx.x","country_code":"FR","country_name":"France","region_code":"A2","region_name":"Bretagne","city":"Brest","zipcode":"","latitude":xxxx,"longitude":xxxx,"metro_code":"","area_code":""}

Any help would be greatly appreciated!

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • doudian7996 doudian7996 7年前
    $a = json_decode($location);
    echo $a->country_name;
    

    You might also want to have a look on this. http://www.php.net/manual/en/function.json-decode.php

    点赞 评论 复制链接分享
  • dongmu5106 dongmu5106 7年前

    Look for manual json_decode

     $tmp = json_decode($location);
        echo $tmp->country_code
    
    点赞 评论 复制链接分享

相关推荐