如何使用php将json数据传输到html? [英] How to transfer json data to html with php?

查看:608
本文介绍了如何使用php将json数据传输到html?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用php将json数据传输到html?

How to transfer json data to html with php?

$url="http://api.nytimes.com/svc/search/v1/article?format=json&query=usa&rank=newest&api-key=mykey"

当我在浏览器中键入URL时,它返回 {"offset" : "0" , "results" : [{"body" : "A guide to cultural and recreational goings-on in and around the Hudson Valley. ...}]} 如何将json body数据放入html中?我的意思是这样echo '<div class="body"></div>';

when I type the url in browser, it return {"offset" : "0" , "results" : [{"body" : "A guide to cultural and recreational goings-on in and around the Hudson Valley. ...}]} how to put the json body data into html? I mean like this echo '<div class="body"></div>';

推荐答案

您首先需要获取文件.您应该为此使用curl.在下面的示例中,我使用了file_get_contents()函数,您可能需要替换它.使用json_decode()为您解析JSON.

You first need to get the file. You should use curl for this. In the example below I used the file_get_contents() function, you might want to replace it. Use json_decode() to parse the JSON for you.

$json = file_get_contents($url);
$data = json_decode($json);
echo $data->results[0]->body;

这将回显A guide to cultural....

这篇关于如何使用php将json数据传输到html?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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