Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/264.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用php将json数据传输到html?_Php_Json - Fatal编程技术网

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

如何使用php将json数据传输到html?,php,json,Php,Json,如何使用php将json数据传输到html $url="http://api.nytimes.com/svc/search/v1/article?format=json&query=usa&rank=newest&api-key=mykey" 当我在浏览器中键入url时,它返回 {“偏移量”:“0”,“结果”:[{“正文”:“哈德逊河谷及其周围的文化和娱乐活动指南…”} 如何将jsonbody数据放入html?我的意思是像这样echo';我不知道你为什么会这样做,但是假

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

$url="http://api.nytimes.com/svc/search/v1/article?format=json&query=usa&rank=newest&api-key=mykey"
当我在浏览器中键入url时,它返回
{“偏移量”:“0”,“结果”:[{“正文”:“哈德逊河谷及其周围的文化和娱乐活动指南…”}

如何将json
body
数据放入html?我的意思是像这样
echo';

我不知道你为什么会这样做,但是假设启用了
fopen()
URL打开,你就可以

echo file_get_contents($url);
像这样

<?php

$url="http://api.nytimes.com/svc/search/v1/article?format=json&query=usa&rank=newest&api-key=mykey";
$json = file_get_contents($url);
echo $json;
对文件内容使用
json\u decode()
,您可以使用
file\u get\u contents($url)
检索文件内容,然后您就有了一个可用于构建HTML的数组

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

if ($dataRaw) {
  $data = json_decode($dataRaw, true);
  foreach ($data['results'] as $cEntry) {
?>
  <div class="body">
      <?php echo $cEntry['body']; ?>
  </div>
<?php
  }
}
$url=”http://api.nytimes.com/svc/search/v1/article?format=json&query=usa&rank=newest&api-key=mykey”;
$dataRaw=文件获取内容($url);
如果($dataRaw){
$data=json_decode($dataRaw,true);
foreach($data['results']作为$cEntry){
?>

首先需要获取文件。应该使用curl。在下面的示例中,我使用了
file\u get\u contents()
函数,您可能需要替换它。使用
json\u decode()
为您解析json

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

这将呼应
文化指南…

一旦您将JSON字符串加载到PHP中,您就可以使用函数
JSON\u decode()
将其转换为PHP数组。然后您可以将适当的数组元素打印到HTML输出中,与任何其他PHP变量一样。

尝试以下操作:

$jsonDecoded = json_decode($yourJsonEncodedData);
echo $jsonDecoded->results->body;

你从哪里调用URL?客户端(JavaScript)还是服务器端(PHP)?你的问题很模糊,你能扩展一下吗?这可以回显
正文
,但如果有10个项目,我将编写
echo$data->results[0]->body;
echo$data->results[1]->body;
,或者使用
foreach($data->results as$result){echo$result->body;}
来回显所有的“body”。非常感谢,我得到了所有的
$result->body
如果你想把你的JSON数据放到网页上,你可以使用客户端脚本,比如JavaScript。例如:
document.getElementById(“”)。innerHTML=
(如果你使用jQuery,这会容易得多)。谢谢,@Orbling,但有一些错误echo
致命错误:无法将stdClass类型的对象作为数组使用
在` foreach($data['results']作为$cEntry){`@cj333:对不起,愚蠢的PHP默认情况下不会将JSON数组设置为实际数组。您必须将第二个参数指定为true以
JSON\u decode()
使其执行此操作。编辑代码。
文件\u get\u contents()
不是从外部URL获取数据的好选项,因为它无法处理超时或在代理后操作。请改用
cURL