【问题标题】:Get all photos from Instagram which have a specific hashtag with PHP [closed]从 Instagram 获取所有带有 PHP 特定标签的照片 [关闭]
【发布时间】:2012-06-25 00:09:02
【问题描述】:

我需要使用 PHP 获取一些带有特定主题标签的图片吗? 任何帮助都会很棒,或者提示?

【问题讨论】:

    标签: php hashtag instagram


    【解决方案1】:

    这是我不久前写的另一个例子:

    <?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>';
        }
    ?>
    

    【讨论】:

    • 你知道有什么方法可以超过 20 的限制吗?
    • @Staysee 请参阅下面的答案。
    • @kexxcream 我正在运行您的 API,我想获取标签媒体 #thesouthfarm。问题是我只得到带有该标签的自己的图像。不是我的朋友。那里有一些限制吗?
    • @kexxcream 抱歉!发现用户有私人资料,因此我无法获取照片! :)
    • 使用最新版本的 Instagram-PHP-API 包装器,您需要将限制传递给 getTagMedia() 方法:$instagram-&gt;getTagMedia($tag, 5);,然后将 foreach 行替换为 foreach($media-&gt;data as $data) {。 (此外,现在加载包装器的最简单方法是使用Composer - 请参阅包装器的 Github 页面上的示例)
    【解决方案2】:

    instagram 公共 API 的标签部分可以帮助您做到这一点。

    【讨论】:

    • 第一个 URL 似乎损坏了。这个有效:instagram.com/developer/endpoints/tags
    • @Shahar 更新中,谢谢。
    • instagram API 刚刚出现 insane。他们真的希望您获得每个访问者的身份验证只是为了获得公共提要吗?
    • 您不需要针对此类请求对用户进行身份验证。您的客户 ID 就足够了。如果您的应用程序代表用户执行某些操作,则它需要用户身份验证。 (评论、点赞、浏览用户的供稿等)。
    • @SimonEast 的目的是您根本不将 API 用于这些目的。他们希望你融入生态系统,而不是从中汲取内容。
    【解决方案3】:

    如果您只需要基于标签显示图像,则无需包含包装类“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.'" />';
        }
    

    【讨论】:

    • 我注意到它只能抓取 20 张图片。这20张照片是最新的吗?如何获得更多?
    • 这些是最新的 20 个。如果您查看 URL,它有 'media/recent'。根据 Instagram API,这将按日期时间降序排列。
    • @Gursharan Singh 你好,我有带下划线的主题标签,我正在使用此代码,除带下划线的主题标签外,所有主题标签都可以正常工作。
    • 但是你真的看到所有的图片吗?我是沙盒模式,只能看到accesstoken-owner的图片?
    • 如何在我的 Android 应用中使用各种主题标签从 Instagram 获取帖子?
    【解决方案4】:

    自 2015 年 11 月 17 日起,您必须对用户进行身份验证才能发出任何请求(甚至例如“获取一些具有特定主题标签的图片”)请求。见the Instagram Platform Changelog

    在 2015 年 11 月 17 日或之后创建的应用: 所有 API 端点都需要有效的 access_token。 2015 年 11 月 17 日之前创建的应用程序: 在 2016 年 6 月 1 日之前不受新 API 行为的影响。

    这使得 2016 年 6 月 1 日之前在此处给出的所有答案不再有用。

    【讨论】:

    • 但是你可以得到“获取一些带有特定标签的图片”without the need of an API
    • 2017,我仍然可以在没有访问令牌的情况下获取数据。示例:instagram.com/salvun/media
    • 如何在我的 Android 应用中使用各种主题标签获取帖子?
    【解决方案5】:

    我已经设置了一个 php 代码,如果您想根据标签访问没有 api 的 Instagram 图像,它可以提供帮助

    Php Code for Instagram hashtag images

    【讨论】:

    【解决方案6】:

    要获得超过 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);
    }
    

    【讨论】:

    • 这很好用! +1如果你有时间,你能看看我遇到的类似问题吗? stackoverflow.com/questions/17487231/…
    • 似乎其他人回答得更快,所以我想它已经解决了。
    • 是的,显然。还是谢谢!
    • 正是我正在寻找的!谢谢 Kexxcream。剩下一个问题。显示的图片数量一直从 13 张图片变为 18 张图片。如何解决?顺便说一句,很奇怪,图片大小不一样。有时我会看到非方形图像!不过剧本不错!爱它!谢谢 kexxcream
    • 无需破解核心。而不是编辑 instagram.class.php 只需设置$instagram-&gt;setClientID('YOUR_CLIENT_ID');
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-17
    • 1970-01-01
    相关资源
    最近更新 更多