2012-06-25 22 views

回答

19

有一個instagram公共API的標籤部分,可以幫助你做到這一點。 http://instagram.com/developer/endpoints/tags/

+0

第一個URL似乎中斷了。這一個作品:http://instagram.com/developer/endpoints/tags/ – Shahar

+0

@Shahar更新,謝謝。 –

+1

Instagram的API只是*瘋了*。他們真的希望你獲得每個訪問者的身份驗證只是爲了獲得公共飼料嗎? –

26

這裏是我寫的一個,而前一個例子:

<?php  
    // Get class for Instagram 
    // More examples here: https://github.com/cosenary/Instagram-PHP-API 
    require_once 'instagram.class.php'; 

    // Initialize class with client_id 
    // Register at http://instagram.com/developer/ and replace client_id with your own 
    $instagram = new Instagram('CLIENT_ID_HERE'); 

    // Set keyword for #hashtag 
    $tag = 'KEYWORD HERE'; 

    // Get latest photos according to #hashtag keyword 
    $media = $instagram->getTagMedia($tag); 

    // Set number of photos to show 
    $limit = 5; 

    // Set height and width for photos 
    $size = '100'; 

    // Show results 
    // Using for loop will cause error if there are less photos than the limit 
    foreach(array_slice($media->data, 0, $limit) as $data) 
    { 
     // Show photo 
     echo '<p><img src="'.$data->images->thumbnail->url.'" height="'.$size.'" width="'.$size.'" alt="SOME TEXT HERE"></p>'; 
    } 
?> 
+0

你知道有什麼辦法超過20的限制嗎? – Staysee

+0

@Staysee請參閱下面的答案。 – kexxcream

+1

@kexxcream我正在運行你的API,我想爲#thesouthfarm標籤獲取TAG媒體。問題是我只能用自己的標籤獲得自己的圖像。不是我的朋友。那裏有一些限制嗎? – Ismailp

5

獲得超過20,你可以使用負載更多的按鈕。

的index.php

<!DOCTYPE html> 
<html> 
<head> 
    <meta charset="utf-8" /> 
    <title>Instagram more button example</title> 
    <!-- 
    Instagram PHP API class @ Github 
    https://github.com/cosenary/Instagram-PHP-API 
    --> 
    <style> 
    article, aside, figure, footer, header, hgroup, 
    menu, nav, section { display: block; } 
    ul { 
     width: 950px; 
    } 
    ul > li { 
     float: left; 
     list-style: none; 
     padding: 4px; 
    } 
    #more { 
     bottom: 8px; 
     margin-left: 80px; 
     position: fixed; 
     font-size: 13px; 
     font-weight: 700; 
     line-height: 20px; 
    } 
    </style> 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> 
    <script> 
    $(document).ready(function() { 
     $('#more').click(function() { 
     var tag = $(this).data('tag'), 
      maxid = $(this).data('maxid'); 

     $.ajax({ 
      type: 'GET', 
      url: 'ajax.php', 
      data: { 
      tag: tag, 
      max_id: maxid 
      }, 
      dataType: 'json', 
      cache: false, 
      success: function(data) { 
      // Output data 
      $.each(data.images, function(i, src) { 
       $('ul#photos').append('<li><img src="' + src + '"></li>'); 
      }); 

      // Store new maxid 
      $('#more').data('maxid', data.next_id); 
      } 
     }); 
     }); 
    }); 
    </script> 
</head> 
<body> 

<?php 
    /** 
    * Instagram PHP API 
    */ 

    require_once 'instagram.class.php'; 

    // Initialize class with client_id 
    // Register at http://instagram.com/developer/ and replace client_id with your own 
    $instagram = new Instagram('ENTER CLIENT ID HERE'); 

    // Get latest photos according to geolocation for Växjö 
    // $geo = $instagram->searchMedia(56.8770413, 14.8092744); 

    $tag = 'sweden'; 

    // Get recently tagged media 
    $media = $instagram->getTagMedia($tag); 

    // Display first results in a <ul> 
    echo '<ul id="photos">'; 

    foreach ($media->data as $data) 
    { 
     echo '<li><img src="'.$data->images->thumbnail->url.'"></li>'; 
    } 
    echo '</ul>'; 

    // Show 'load more' button 
    echo '<br><button id="more" data-maxid="'.$media->pagination->next_max_id.'" data-tag="'.$tag.'">Load more ...</button>'; 
?> 

</body> 
</html> 

ajax.php

<?php 
    /** 
    * Instagram PHP API 
    */ 

    require_once 'instagram.class.php'; 

     // Initialize class for public requests 
     $instagram = new Instagram('ENTER CLIENT ID HERE'); 

     // Receive AJAX request and create call object 
     $tag = $_GET['tag']; 
     $maxID = $_GET['max_id']; 
     $clientID = $instagram->getApiKey(); 

     $call = new stdClass; 
     $call->pagination->next_max_id = $maxID; 
     $call->pagination->next_url = "https://api.instagram.com/v1/tags/{$tag}/media/recent?client_id={$clientID}&max_tag_id={$maxID}"; 

     // Receive new data 
     $media = $instagram->getTagMedia($tag,$auth=false,array('max_tag_id'=>$maxID)); 

     // Collect everything for json output 
     $images = array(); 
     foreach ($media->data as $data) { 
     $images[] = $data->images->thumbnail->url; 
     } 

     echo json_encode(array(
     'next_id' => $media->pagination->next_max_id, 
     'images' => $images 
    )); 
?> 

instagram.class.php

查找功能getTagMedia()和替換爲:

public function getTagMedia($name, $auth=false, $params=null) { 
    return $this->_makeCall('tags/' . $name . '/media/recent', $auth, $params); 
} 
+0

這很好用! +1如果你有時間,你可以看看我有類似的問題嗎? http://stackoverflow.com/questions/17487231/input-value-in-ajax-call-does-not-work –

+0

似乎有人設法回答更快,所以我想它已經解決了。 – kexxcream

+0

是的,顯然。不管怎麼說,還是要謝謝你! –

15

如果你只需要顯示基於標籤的圖像,那麼不包括包裝類「instagram.class.php」。由於媒體& Instagram API中的標籤端點不需要認證。您可以使用以下基於curl的功能根據您的標籤檢索結果。

function callInstagram($url) 
    { 
    $ch = curl_init(); 
    curl_setopt_array($ch, array(
    CURLOPT_URL => $url, 
    CURLOPT_RETURNTRANSFER => true, 
    CURLOPT_SSL_VERIFYPEER => false, 
    CURLOPT_SSL_VERIFYHOST => 2 
    )); 

    $result = curl_exec($ch); 
    curl_close($ch); 
    return $result; 
    } 

    $tag = 'YOUR_TAG_HERE'; 
    $client_id = "YOUR_CLIENT_ID"; 
    $url = 'https://api.instagram.com/v1/tags/'.$tag.'/media/recent?client_id='.$client_id; 

    $inst_stream = callInstagram($url); 
    $results = json_decode($inst_stream, true); 

    //Now parse through the $results array to display your results... 
    foreach($results['data'] as $item){ 
     $image_link = $item['images']['low_resolution']['url']; 
     echo '<img src="'.$image_link.'" />'; 
    } 
+0

我注意到它只抓取了20張圖片。那20張最新的照片?如何獲得更多? – dKab

+0

這些是最新的20.如果你看看網址,它有'媒體/最近'。根據Instagram API,這將按日期時間降序排列。 – mirichan

+0

@Gursharan Singh你好我有帶下劃線的hashtag,我使用這段代碼,並且對所有hashtag都可以正常工作,除了hashtag和下劃線的任何幫助。 – Fadi

13

自2015年11月17日,你必須進行身份驗證的用戶製作任何(甚至諸如「獲得具有特定主題標籤的一些照片」)請求。見the Instagram Platform Changelog:於2015年11月17日或之後創建

應用: 所有API端點需要一個有效的access_token。 2015年11月17日之前創建的 應用: 不受新的API的行爲,直到6月1日,2016年

現在這使得2016不再有用的6月1日之前,這裏給出所有答案。

+0

,但你可以得到「獲得一些具有特定標籤的圖片」[不需要API](http://stackoverflow.com/questions/40542335/how-to-get-all-images-of-hashtag-in -instagram-without-api) – machineaddict

+0

2017,我仍然可以在沒有訪問令牌的情況下獲取數據。示例:https://www.instagram.com/salvun/media/ – redochka

+0

我如何在我的android應用程序中使用各種主題標籤獲取帖子? –

相關問題