如何从一个jsonfile中只回显一个帖子


How to echo only one post from a jsonfile

我制作了一个模块,可以显示instagram个人资料中的最新图像,它在Joomla 3.4.8中使用一个小的弹出库非常完美我想添加个人资料图像profile_picture,但它当然应该只出现一次。我尝试过各种php echo,但没有运气

有人能帮忙吗?

PHP

<?php
    // Supply a user id and an access token
    $userid = $params->get('klintweb_insta_id');
    $accessToken = $params->get('klintweb_insta_access_token');
    $count = $params->get('klintweb_insta_count');
    // Gets post and image
    function fetchData($url){
         $ch = curl_init();
         curl_setopt($ch, CURLOPT_URL, $url);
         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
         curl_setopt($ch, CURLOPT_TIMEOUT, 20);
         $result = curl_exec($ch);
         curl_close($ch); 
         return $result;
    }
    // Pulls and parses data.
    $result = fetchData("https://api.instagram.com/v1/users/{$userid}/media/recent/?count={$count}&access_token={$accessToken}");
    $result = json_decode($result);

?>

前臂

<?php foreach ($result->data as $post): ?>
    {emailcloak=off}
    <!-- Renders images. @Options (thumbnail,low_resoulution, high_resolution) -->
    <span class="KlintWebInstaSpan">
        <a data-mediabox-group="insta" type="image/jpeg" class="jcepopup noicon" data-mediabox-caption="<?= rtrim(strip_tags(substr ($post->caption->text,0,140))).'...'; ?>" data-mediabox-title="rasmusjorgensen128" href="<?= $post->images->standard_resolution->url ?>">
            <img src="<?= $post->images->thumbnail->url ?>" alt="Billede fra instagram" />
            <span class="overlay"></span>
        </a>
    </span>
<?php endforeach ?>

jsonfile

{
"data": [{
    "comments": {
        "count": 0
    },
    "caption": {
        "created_time": "1296710352",
        "text": "Inside le truc #foodtruck",
        "from": {
            "username": "kevin",
            "full_name": "Kevin Systrom",
            "type": "user",
            "id": "3"
        },
        "id": "26621408"
    },
    "likes": {
        "count": 15
    },
    "link": "http://instagr.am/p/BWrVZ/",
    "user": {
        "username": "kevin",
        "profile_picture": "http://distillery.s3.amazonaws.com/profiles/profile_3_75sq_1295574122.jpg",
        "id": "3"
    },
    "created_time": "1296710327",
    "images": {
        "low_resolution": {
            "url": "http://distillery.s3.amazonaws.com/media/2011/02/02/6ea7baea55774c5e81e7e3e1f6e791a7_6.jpg",
            "width": 306,
            "height": 306
        },
        "thumbnail": {
            "url": "http://distillery.s3.amazonaws.com/media/2011/02/02/6ea7baea55774c5e81e7e3e1f6e791a7_5.jpg",
            "width": 150,
            "height": 150
        },
        "standard_resolution": {
            "url": "http://distillery.s3.amazonaws.com/media/2011/02/02/6ea7baea55774c5e81e7e3e1f6e791a7_7.jpg",
            "width": 612,
            "height": 612
        }
    },
    "type": "image",
    "users_in_photo": [],
    "filter": "Earlybird",
    "tags": ["foodtruck"],
    "id": "22721881",
    "location": {
        "latitude": 37.778720183610183,
        "longitude": -122.3962783813477,
        "id": "520640",
        "street_address": "",
        "name": "Le Truc"
    }
},
{
    "videos": {
        "low_resolution": {
            "url": "http://distilleryvesper9-13.ak.instagram.com/090d06dad9cd11e2aa0912313817975d_102.mp4",
            "width": 480,
            "height": 480
        },
        "standard_resolution": {
            "url": "http://distilleryvesper9-13.ak.instagram.com/090d06dad9cd11e2aa0912313817975d_101.mp4",
            "width": 640,
            "height": 640
        },
    "comments": {
        "count": 2
    },
    "caption": null,
    "likes": {
        "count": 1
    },
    "link": "http://instagr.am/p/D/",
    "created_time": "1279340983",
    "images": {
        "low_resolution": {
            "url": "http://distilleryimage2.ak.instagram.com/11f75f1cd9cc11e2a0fd22000aa8039a_6.jpg",
            "width": 306,
            "height": 306
        },
        "thumbnail": {
            "url": "http://distilleryimage2.ak.instagram.com/11f75f1cd9cc11e2a0fd22000aa8039a_5.jpg",
            "width": 150,
            "height": 150
        },
        "standard_resolution": {
            "url": "http://distilleryimage2.ak.instagram.com/11f75f1cd9cc11e2a0fd22000aa8039a_7.jpg",
            "width": 612,
            "height": 612
        }
    },
    "type": "video",
    "users_in_photo": null,
    "filter": "Vesper",
    "tags": [],
    "id": "363839373298",
    "user": {
        "username": "kevin",
        "full_name": "Kevin S",
        "profile_picture": "http://distillery.s3.amazonaws.com/profiles/profile_3_75sq_1295574122.jpg",
        "id": "3"
    },
    "location": null
},

]}

感谢Adam Taylor你指出可以做得多么简单

也感谢Danoweb帮助

我理解得对吗?你正在循环浏览照片列表,并且还想访问个人资料图片并打印一次?

你需要循环浏览所有的照片吗?如果没有,您可以在打印照片后添加一个break,以退出循环。

如果你也需要循环浏览照片,为什么不访问JSON中的第一个元素,从那里获取个人资料图片,然后循环浏览其余的照片?

编辑:就您的解决方案而言,直接访问第一张照片,打印相关的用户缩略图,然后继续循环并打印所有照片可能会更容易。类似于:

<div class="user">
  <?php $post = $result->data[0]; ?>
    <img src="<?= $post->user->profile_picture ?>" alt="<?= $post->user->username ?>" />
    <span class="username"><?= $post->user->username ?></span>
</div>
<!-- do the rest of your looping/displaying photos -->

在您的解决方案中,您将数组切片为一个元素,然后当您真正想做的只是访问第一个元素时,在数组上循环。

据我所知,配置文件图片似乎在JSON响应中提供了两次。我会使用一个变量来跟踪我是否已经显示了个人资料图片,如果是,就不要显示第二个。例如:

<?php
$shown_profile = false;
foreach ($result->data as $post)
{
    //WE HAVE NOT SHOWN PROFILE PIC, SHOW IT AND SET FLAG.
    if($shown_profile == false and (image is profile pic) )
    {
        $shown_profile = true;
    }
    //WE HAVE ALREADY SHOWN PROFILE PIC AND THIS IMAGE IS A DUPLICATE
    if($shown_profile == true and (image is profile pic) )
    {
        //JUMP TO THE NEXT LOOP INTERATION
        continue;
    }
    //[THE REST OF YOUR FOREACH CODE]
}
?>

您可以在此处找到有关"继续"语句的更多信息:
http://php.net/manual/en/control-structures.continue.php

希望这能有所帮助!