简体   繁体   English

在 PHP 中使用 json 响应

[英]Using json response in PHP

I am trying to use one object from the many provided in my json request.我正在尝试使用我的 json 请求中提供的许多对象中的一个。

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!任何帮助将不胜感激!

Look for manual json_decode寻找手动json_decode

 $tmp = json_decode($location);
    echo $tmp->country_code
$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 http://www.php.net/manual/en/function.json-decode.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM