Instagram加载更多按钮不起作用

时间:2015-12-15 15:23:47

标签: php jquery instagram

 <html>
<head>
  <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
  </head>
  <body>

<script>
    $(document).ready(function() {
      $('#more').click(function() {
        var tag   = $(this).data('tag'),
            maxid = $(this).data('maxid'),

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

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


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

        require_once 'Instagram.php';
          use MetzWeb\Instagram\Instagram;



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

        // $geo = $instagram->searchMedia(56.8770413, 14.8092744);

        $tag = 'malaysia';

        // 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" name="max_id" data-maxid="'.$media->pagination->next_max_id.'" data-tag="'.$tag.'">Load more ...</button>';
    ?>

上面的代码是index.php代码,其中显示图片的网站以及加载更多按钮功能的位置。

<?php
    /**
     * Instagram PHP API
     */
      set_include_path("../src/" . PATH_SEPARATOR . get_include_path());
      require_once 'Instagram.php';
      use MetzWeb\Instagram\Instagram;

      // Initialize class for public requests
      $instagram = new Instagram('xxxxxxx');

      // 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
      ));
?>

这部分代码是pagination.php部分,其中获取max_id和那些东西以及运行加载更多按钮脚本的代码。我遇到的问题实际上是当我运行代码时,index.php部分确实显示了图像,但是当我运行加载更多按钮时它不起作用,当我去pagination.php时出现了这个错误

Notice: Undefined index: tag in /Applications/XAMPP/xamppfiles/htdocs/instagram search/pagination.php on line 13

    Notice: Undefined index: max_id in /Applications/XAMPP/xamppfiles/htdocs/instagram search/pagination.php on line 14

    Warning: Creating default object from empty value in /Applications/XAMPP/xamppfiles/htdocs/instagram search/pagination.php on line 18

    Notice: Undefined index: X-Ratelimit-Remaining in /Applications/XAMPP/xamppfiles/htdocs/instagram search/Instagram.php on line 633

    Notice: Trying to get property of non-object in /Applications/XAMPP/xamppfiles/htdocs/instagram search/pagination.php on line 26

    Warning: Invalid argument supplied for foreach() in /Applications/XAMPP/xamppfiles/htdocs/instagram search/pagination.php on line 26

    Notice: Trying to get property of non-object in /Applications/XAMPP/xamppfiles/htdocs/instagram search/pagination.php on line 31

    Notice: Trying to get property of non-object in /Applications/XAMPP/xamppfiles/htdocs/instagram search/pagination.php on line 31
    {"next_id":null,"images":[]}

你们能帮帮我吗,你们的帮助将会非常感激!

1 个答案:

答案 0 :(得分:0)

这部分:

    var tag   = $(this).data('tag'),
        maxid = $(this).data('maxid'),

应该是:

    var tag   = $(this).data('tag'),
        maxid = $(this).data('maxid');

(最后有一个;,而不是,)。

这部分:

      data: {
        "tag=" +tag,
        "max_id=" +maxid
      },

应替换为:

      data: {
        "tag": tag,
        "max_id": maxid
      },

(之前的语法没有意义)。