爱学习的小朋友 2022-05-04 16:31 采纳率: 88.9%
浏览 78
已结题

PHP如何将下面的json部分数据渲染到html中

PHP如何把下面链接请求到的json在HTML中渲染出来呢?
https://pixabay.com/api/?key=20762951-7eebd38ed836f5a1103ccfe38&q=yellow+flowers&image_type=photo

例如把json中的userImageURL值填入图片src中,user值填入到p标签中,然后遍历出来。

<div>
    <ul class="imglb">
        <li><img src="json userImageURL值"   ><p>json user值</p></li>
        <li><img src="json userImageURL值"   ><p>json user值</p></li>
        <li><img src="json userImageURL值"   ><p>json user值</p></li>
        <li><img src="json userImageURL值"   ><p>json user值</p></li>
        <li><img src="json userImageURL值"   ><p>json user值</p></li>
    </ul>
</div>
<style>
.imglb{
    width:100%;
}
.imglb li{
    list-style:none;
    float: left;
    text-align: center;
    margin: 10px;
}
</style>

  • 写回答

3条回答 默认 最新

  • CSDN专家-showbo 2022-05-04 16:40
    关注

    file_get_contents获取网址内容(需要配置php.ini开启openssl,要不会报错file_get_contents(): Unable to find the wrapper &quot;https&quot; - did you forget to enable it when you configured PHP,去掉extension=openssl注释),json_decode转为对象后遍历就行。如果无法修改php.inc,可以用curl代替

    img

    <meta charset="utf-8">
    <?php
    
    
    $s=file_get_contents("https://pixabay.com/api/?key=20762951-7eebd38ed836f5a1103ccfe38&q=yellow+flowers&image_type=photo&pretty=true");
    $o=json_decode($s,true);
    ?>
    <div>
        <ul class="imglb">
        <?php
        foreach($o['hits'] as $item){
          echo "<li><img src=\"".$item['userImageURL']."\"><p>".$item['user']."</p></li>";
        }
        ?>
        </ul>
    </div>
    <style>
    .imglb{
        width:100%;
    }
    .imglb li{
        list-style:none;
        float: left;
        text-align: center;
        margin: 10px;
    }
    </style>
    

    img

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 5月12日
  • 已采纳回答 5月4日
  • 创建了问题 5月4日

悬赏问题

  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来