Instagram APIのAjaxページネーションを修正しようとしています
20枚の写真のみを表示。「さらに読み込む」ボタンが機能しませんでした。
コンソールで:
Uncaught ReferenceError: maxid is not defined
index.php は次のとおりです。
<script>
$(document).ready(function() {
$('#more').click(function() {
var max_id = $(this).data('nextid');
$.ajax({
type: 'GET',
url: 'ajax.php',
data: {
max_id: maxid
},
dataType: 'json',
cache: false,
success: function(data) {
// Output data
$.each(data.images, function(i, src) {
$('#photos').append('<li><img src="' + src + '"></li>');
});
// Store new maxid
$('#more').data('maxid', data.next_id);
}
});
});
});
</script>
<?php
require_once 'instagram.class.php';
$instagram = new Instagram(array(
'apiKey' => '*****',
'apiSecret' => '*****',
'apiCallback' => '******'
));
// Receive OAuth code parameter
$code = $_GET['code'];
if (true === isset($code)) {
$data = $instagram->getOAuthToken($code);
$instagram->setAccessToken($data);
$media = $instagram->getUserMedia();
}
?>
<ul id="photos">
<?php foreach( $media->data as $data ): ?>
<li><img src="<?php echo $data->images->thumbnail->url ?>"></li>
<?php endforeach; ?>
<?php echo "<br><button id=\"more\" data-maxid=\"{$media->pagination->next_max_id}\">Load more ...</button>"; ?>
ajax.php:
require_once 'instagram.class.php';
$instagram = new Instagram(array(
'apiKey' => '****',
'apiSecret' => '*****',
'apiCallback' => '*******'
));
// Receive OAuth code parameter
$code = $_GET['code'];
if (true === isset($code)) {
$data = $instagram->getOAuthToken($code);
// Initialize class for public requests
$instagram->setAccessToken($data);
$media = $instagram->getUserMedia();
// Receive AJAX request and create call object
$maxID = $_GET['next_max_id'];
$clientID = $instagram->getApiKey();
$call = new stdClass;
$call->pagination->next_max_id = $maxID;
$call->pagination->next_url = "https://api.instagram.com/v1/users/self/media/recent?client_id={$clientID}&max_id={$maxID}";
// Receive new data
$media = $instagram->pagination($call);
// 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,
'max_id' => $media->pagination->max_id,
'images' => $images
));
https://github.com/cosenary/Instagram-PHP-APIを使用しています
ありがとう!