【问题标题】:How to get my recent photos from Instagram?如何从 Instagram 获取我最近的照片?
【发布时间】:2016-07-07 13:59:15
【问题描述】:

我阅读了 instargam API 并在 google 中搜索了代码,但没有得到任何符合我要求的确切解决方案。我想在我的网站上显示我最近的照片,比如 Facebook 插件。我的图像看起来像 -

我尝试了以下 URL,但出现错误。你能检查一下 -

https://api.instagram.com/v1/tags/nofilter/media/recent?access_token=fb2e77d.47a0479900504cb3ab4a1f626d174d2d

https://api.instagram.com/v1/users/user-id/media/recent/?access_token=72020554b1884a9691352d4cd9759951

请建议我如何显示我最近的照片看起来像上面的截图?

编辑: 我有Client IDClient Secreat。告诉我Access Token是哪一个?

【问题讨论】:

  • Instagram API 返回一个 JSON 对象 - 您需要对其进行迭代以获取图像 ID,然后自己创建布局。 JSON 中没有显示格式,除了图像大小选项。您的第二个 URL 是获取您自己的图像的正确终点,但您需要您的用户 ID(编号)和正确的访问令牌(其格式与您第一个 URL 中的格式相同)。

标签: instagram instagram-api


【解决方案1】:

我用过这个API 请检查。我相信这会对你有所帮助。

<?php
$access_token = 'YOUR_3rd_PARTY_ACCESS_TOKEN';
$user_id = 'INSTAGRAM_USER_ID';
$json_profile = file_get_contents("https://api.instagram.com/v1/users/{$user_id}/?access_token={$access_token}");
$json = file_get_contents("https://api.instagram.com/v1/users/{$user_id}/media/recent/?access_token=" . $access_token . "&count=9");
$a_json_profile = json_decode($json_profile, true);
$a_json = json_decode($json, true);
//echo "<pre>";
//print_r($json2);
//echo "</pre>"; exit;
$i = 0;
foreach ($a_json['data'] as $key => $value) {
    //if ($i < 9) {
    $a_images[$i]['link'] = $value['link'];
    $a_images[$i]['url'] = $value['images']['thumbnail']['url'];
    //$a_images[$value['id']]['caption'] = $value['caption']['text'];

    $i++;
    //}
}
shuffle($a_images);
$finalData = array('info'=>$a_json_profile, 'images'=>$a_images);
echo json_encode($finalData);
exit;

【讨论】:

    【解决方案2】:

    fb2e77d.47a0479900504cb3ab4a1f626d174d2d -> 这是您需要获取自己的访问令牌的 Instagram 示例。

    替换数组中的值。

    'client_id' ; 'client_secret'; 'redirect_uri' 和 'code'

    client_id:您的客户端 ID
    client_secret:您的客户端密码
    grant_type:authorization_code 是目前唯一支持的值redirect_uri:您在授权请求中使用的redirect_uri。注意:这必须与授权请求中的值相同。
    代码:您在授权步骤中收到的确切代码。

    阅读官方文档(Instagram Dev. ) 第一步:将您的用户引导至我们的授权 URL 和第二步:接收来自 Instagram 的重定向(您在此处获取 CODE)。

    $apiData = array(
      'client_id'       => $client_id,
      'client_secret'   => $client_secret,
      'grant_type'      => 'authorization_code',
      'redirect_uri'    => $redirect_uri,
      'code'            => $code
    );
    
    $apiHost = 'https://api.instagram.com/oauth/access_token';
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $apiHost);
    curl_setopt($ch, CURLOPT_POST, count($apiData));
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($apiData));
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json'));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $jsonData = curl_exec($ch);
    curl_close($ch);
    
    var_dump($jsonData);
    $user = @json_decode($jsonData); 
    
    
    print_r($user);
    

    您可以查看官方页面。 Instagram Dev.

    【讨论】:

    • 这个 sn-p 是为了请求 access_token 而不是获取 feed 本身。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多