一页上有多个 Instagram 提要

Mulitple Instagram feeds on one page

我已经成功地将特定用户的 Instagram 提要放到了我的网站上,但是我对 PHP 的经验很少,我不知道如何简单地重复这个过程。我希望同时展示两个不同的用户 div.

<?php

// http://jelled.com/instagram/lookup-user-id/
$userid = "userid";


// http://instagram.com/developer/
$clientid = "clientid";


// http://jelled.com/instagram/access-token/
$accessToken = "token";


// number of photos to return
$count = "4";

// Gets our data
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/?access_token={$accessToken}&count={$count}");
$result = json_decode($result);

// cycles through the json tree and uses the low res url in the img tag
echo "<ul>";
    foreach ($result->data as $photo) {
        $img = $photo->images->{$display_size="thumbnail"};
        echo "<li><a href='{$photo->link}'><img src='{$img->url}' /></a></li>";

    }
    echo "</ul>";

?>

如果我再次粘贴代码,整个页面将停止工作。我猜这很简单,但我不知道我在找什么!这段代码是否应该放在一个单独的文件中,然后 link 编辑到我的网站中,而不是将一些 PHP 放入 HTML Bootstrap 网站?

提前致谢。

编辑

我能够通过使用下面的答案来完成这项工作。我希望每个帐户都有自己的 div,而我知道如何做到这一点的唯一方法是在 html 文件中——这意味着我仍然需要 link 到两个不同的文件.我用正确的代码创建了一个文件,另一个用这个:

<?php


// Set User ID here for different profile
//$userid = "idHere";
$userid = "296517730";

    $result = fetchData("https://api.instagram.com/v1/users/{$userid}/media/recent/?client_id={$clientid}&count={$count}");
$result = json_decode($result);

// cycles through the json tree and uses the low res url in the img tag
echo "<ul>";
    foreach ($result->data as $photo) {
        $img = $photo->images->{$display_size="thumbnail"};
        echo "<li><a href='{$photo->link}'><img src='{$img->url}' /></a></li>";

    }
    echo "</ul>";





?>

它在我的域上工作得很好,但是当我将它移到我客户的域时,我收到了这个错误:警告:为 /home/savenors/savenorsmarket.[=40= 中的 foreach() 提供的参数无效].php 第 53 行

发生了什么事?我猜我为使它正常工作所做的一切都没有真正起作用……但对我来说看起来不错。有任何想法吗?这是网站:http://www.savenorsmarket.com

这是在我的机器上运行两次的代码。它两次提取相同的用户图片,但要解决此问题,只需在第二次调用 fetchData() 之前重置用户 ID 变量;

<?php
$userid = "idHere";


// http://instagram.com/developer/
$clientid = "IDhere";




// number of photos to return
$count = "4";

// Gets our data
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/?client_id={$clientid}&count={$count}");
$result = json_decode($result);

// cycles through the json tree and uses the low res url in the img tag
echo "<ul>";
    foreach ($result->data as $photo) {
        $img = $photo->images->{$display_size="thumbnail"};
        echo "<li><a href='{$photo->link}'><img src='{$img->url}' /></a></li>";

    }
    echo "</ul>";


// Set User ID here for different profile
//$userid = "idHere";

    $result = fetchData("https://api.instagram.com/v1/users/{$userid}/media/recent/?client_id={$clientid}&count={$count}");
$result = json_decode($result);

// cycles through the json tree and uses the low res url in the img tag
echo "<ul>";
    foreach ($result->data as $photo) {
        $img = $photo->images->{$display_size="thumbnail"};
        echo "<li><a href='{$photo->link}'><img src='{$img->url}' /></a></li>";

    }
    echo "</ul>";





?>

另请注意,我使用 client_id 而不是 access_token。不管怎样它都应该工作。