简体   繁体   中英

Using json response in PHP

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!

Look for manual 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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