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

时间:2016-01-13 15:49:14

标签: php json echo instagram

我制作了一个模块,显示来自Instagram个人资料的最新图像,并且在Joomla 3.4.8中使用一个小弹出库完美运行 我想添加个人资料图片 - profile_picture,但当然它应该只出现一次。 我尝试了各种各样的PHP回声 - 但没有运气

有人可以帮忙吗?

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);


?>

foreach

<?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的帮助

2 个答案:

答案 0 :(得分:1)

我是否正确理解您正在浏览照片列表,并且您还希望访问个人资料照片并打印一次?

你需要遍历所有照片吗?如果没有,您可以在打印照片后添加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 -->

在你的解决方案中,你将数组切割成一个元素,然后在你真正想做的就是访问第一个元素时循环遍历数组。

答案 1 :(得分:0)

根据我的理解,看起来在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]
}

?>

您可以找到有关&#34;继续&#34;的更多信息。声明:
http://php.net/manual/en/control-structures.continue.php

希望这有帮助!