【问题标题】:Instagram widget invalid returnInstagram 小部件无效返回
【发布时间】:2018-05-01 18:44:36
【问题描述】:

我想在我的网站上展示 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 ?>

【问题讨论】:

  • var 转储 $result。这是什么?
  • vardump 返回 null
  • 你应该考虑使用这个包吗? github.com/vinkla/instagram
  • 所以请求失败。看看 curl_error() 给你什么
  • 卷曲错误什么也没给我

标签: php instagram


【解决方案1】:

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

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

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

【讨论】:

  • 对于你自己的访问令牌,这工作得很好,你能详细说明一下,究竟是什么不适合你?
【解决方案2】:

如果我是对的,你只有 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-26
    • 2016-11-04
    • 2020-08-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多