Instagram 小部件无效 return

Instagram widget invalid return

我想在我的网站上显示 Instagram 照片,使用 PHP7。 我已经注册了我的应用程序,并从 Instagram 添加了 clientId 和 secretId。但是下面的代码给了我错误:data is aof non object and foreach is an invalid argument.

<?php
    // Supply a user id and an access token
    $clientid = "my client id";
    $accessToken = "my client secret";

    // 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/{$clientid}/media/recent/?access_token={$accessToken}");
    $result = json_decode($result);
?>



    <?php foreach ($result->data as $post): ?>
        <!-- Renders images. @Options (thumbnail,low_resoulution, high_resolution) -->
        <a class="group" rel="group1" href="<?= $post->images->standard_resolution->url ?>"><img src="<?= $post->images->thumbnail->url ?>"></a>
    <?php endforeach ?>

您可以将此 api 用于自己,不能用于任何其他用户。

https://api.instagram.com/v1/users/self/media/recent/?access_token=ACCESS-TOKEN

参考这个 - https://www.instagram.com/developer/endpoints/users/

如果我是对的,你只有 Client ID & Client Secret,但你没有 Access Token

跟着我的脚步走

首先,您必须使用 Client ID & Client Secret

创建 access_token

访问以下网站之一,制作access_token

或者您可以登录 instagram 帐户并创建您的 access_token

创建 access_token 后,格式如下:

xxxxxxxxxx.xxxxxxx.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

然后,在您的控制器函数中添加以下代码

$access_token = 'xxxxxxxxxx.xxxxxxx.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'; // your access_token will be this format
$count_feed = 2; // how many feed want to load

$requestURL = 'https://api.instagram.com/v1/users/self/media/recent?access_token='.$access_token.'&count='.$count_feed;
$ch = curl_init();
curl_setopt_array($ch, array(        
    CURLOPT_URL => $requestURL,
    CURLOPT_HEADER  => false,
    CURLOPT_RETURNTRANSFER => 1
));
$json_response = curl_exec($ch);
curl_close($ch);
$insta_feeds = json_decode($json_response, true);

_e($insta_feeds); // $insta_feeds['data'] will return feed

您可以使用 Instagram 库获取提要。在此处查看图书馆:https://github.com/suhindra/CodeIgniter-Instagram-API