首页 > 解决方案 > 是否可以从 JSON 文件中获取 URL 以显示来自 img src 的图像

问题描述

(注意:json 新手)我有一个 JSON 文件,其中包含图像 URL(https://pastebin.com/TavfuziF),代码如下:

 <!DOCTYPE html>
<html>
  
<body>
    <center>
<?php 

$jdata = file_get_contents("cats-in-moviesnew.json");
//decode the retrieved json file into an array
$contents = json_decode($jdata, true);

echo
//create the table
"<table style border=3px solid black; ><tr><th>Producer</th><th>Director</th><th>Title</th><th>Year made</th><th>URL</th><th>Image</th><th>Poster URL</th></tr>";


//filling the table
foreach($contents as $record) {
echo
"<tr>
<td>" . $record["fields"]["produced_by"] . "</td>
<td>" . $record["fields"]["directed_by"] . "</td>
<td>" . $record["fields"]["title"] . "</td>
<td>" . $record["fields"]["year"] . "</td>
<td>" . $record["fields"]["url"] . "</td>
<td>" . 

$record["fields"]["url_poster"]

. "</td>
<td>" . $record["fields"]["url_poster"] . "</td>

</tr>";
}
?>
</body>
</center>
  
</html>

并且我一直试图弄清楚是否有可能在该代码中执行类似 <img src="$record["fields"]["url_poster"]"> (当前间隙在表格填充位中)之类的操作或不。我已经尝试了几件对我没有用的事情,我看到过关于将东西转换为 base64 的事情,但在我的水平上这对我来说非常复杂,我现在不知道还能做什么。任何帮助将不胜感激。

标签: phphtmljson

解决方案


没关系,我发现只需要在 $record["fields"]["url_poster"] 之前添加 {},所以它看起来像:"<img src={$record["fields"]["url_poster"]}>"


推荐阅读