2015-12-15 83 views
0
<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代碼,其中站點顯示圖片以及在哪裏加載更多的按鈕功能。Instagram加載更多按鈕無法正常工作

<?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":[]} 

你們可以幫我出去請幫助我們將不勝感激!

回答

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 
     }, 

(以前的語法只是毫無意義的)。

+0

我嘗試它仍然不能正常工作 – user3711175

+0

究竟發生了什麼?你是否得到了同樣的錯誤,或者其他什麼?如果是這樣,什麼?你能記錄$ _GET的內容嗎? – jcaron

+0

Uncaught語法錯誤:load.php上的意外標記第13行.ajax({部分 – user3711175