douci4026 2019-04-22 09:09
浏览 30

PHP:使用来自JSON格式的curl获取图像

I want to fetch The Image from the JSON format using cURL PHP.
SO I Have created a code through which I am able to fetch the required Image.

The JSON values are in this kind of Format :

[
{
"albumId": 1,
"id": 1,
"title": "accusamus beatae ad facilis cum similique qui sunt",
"url": "https://via.placeholder.com/600/92c952",
"thumbnailUrl": "https://via.placeholder.com/150/92c952"
},
{
"albumId": 1,
"id": 2,
"title": "reprehenderit est deserunt velit ipsam",
"url": "https://via.placeholder.com/600/771796",
"thumbnailUrl": "https://via.placeholder.com/150/771796"
},
{
"albumId": 1,
"id": 3,
"title": "officia porro iure quia iusto qui ipsa ut modi",
"url": "https://via.placeholder.com/600/24f355",
"thumbnailUrl": "https://via.placeholder.com/150/24f355"
},
]
 ... and so on

The php Code I used is Shown Below.

$url = "https://jsonplaceholder.typicode.com/albums/1/photos";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);

curl_close($ch);
$albumId = 1;
$id = 3;
$image_data = json_decode($result);
foreach($image_data as $image) {
    if ($image - > id == $id && $image - > albumId == $albumId) {
        echo "<img src=".$image - > url.">";
        echo "<br/>";
        break;
    }
}

I am getting the Result that I want but . is this the correct way to implement the code. or are there some loopholes in it. If there, please mention it.

I need the Best Practice available for this Process.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥30 Unity接入微信SDK 无法开启摄像头
    • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
    • ¥20 cad图纸,chx-3六轴码垛机器人
    • ¥15 移动摄像头专网需要解vlan
    • ¥20 access多表提取相同字段数据并合并
    • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
    • ¥20 Java-Oj-桌布的计算
    • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
    • ¥20 有人知道这种图怎么画吗?
    • ¥15 pyqt6如何引用qrc文件加载里面的的资源