通过URL进行JSON解码 [英] JSON decoding from a URL

查看:192
本文介绍了通过URL进行JSON解码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个返回JSON字符串的API:

I'm using an API which returns a JSON string:

http://api.geonames.org /findNearbyPlaceNameJSON?lat=51.9877644&lng=-1.47866&username=demo

我感兴趣的部分是城市/城镇名称(在本例中为Hook Norton),我希望将其用作PHP变量.我知道JSON_decode()函数在某处起作用,但是如何访问API的输出?

The part I'm interested in is the city/town name (in this instance Hook Norton) which I'd like as a PHP variable. I understand the JSON_decode() function comes into play somewhere, but how do I access the output of the API?

推荐答案

尝试一下.

$json = file_get_contents('http://api.geonames.org/findNearbyPlaceNameJSON?lat=51.9877644&lng=-1.47866&username=demo');

$data = json_decode($json,true);

$Geonames = $data['geonames'][0];

echo "<pre>";

print_r($Geonames);

exit;

这篇关于通过URL进行JSON解码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆