Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Instagram PHP API - How to implement a load more button (AJAX)

Instagram PHP API

How to use

index.php file

Enter your Instagram client id and display the first results.
Then store the next_max_id which you receive if you call:

$media->pagination->next_max_id;

You'll require this and the tag name later on, if you make your AJAX request.

ajax.php file

This file delivers more images via pagination.
It requires two GET parameters:

  • tag the tag name you used in your original request (index.php file)
  • max_id next max id

Example call: http://example.com/ajax.php?tag=kitty&max_id=1340220451361

Credits

Copyright (c) 2011-2012 - Programmed by Christian Metz
Released under the BSD License.

<?php
/**
* Instagram PHP API
*
* @link https://github.com/cosenary/Instagram-PHP-API
* @author Christian Metz
* @since 20.06.2012
*/
require 'Instagram.php';
use MetzWeb\Instagram\Instagram;
// Initialize class for public requests
$instagram = new Instagram('12345678');
// 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}";
$media = $instagram->pagination($call);
echo 'Max ID: ' . $media->pagination->next_max_id;
// Return next results
foreach ($media->data as $data) {
echo "<img src=\"{$data->images->thumbnail->url}\">";
}
<?php
/**
* Instagram PHP API
*
* @link https://github.com/cosenary/Instagram-PHP-API
* @author Christian Metz
* @since 20.06.2012
*/
require 'Instagram.php';
use MetzWeb\Instagram\Instagram;
// Initialize class for public requests
$instagram = new Instagram('12345678');
$tag = 'kitty';
// Get recently tagged media
$media = $instagram->getTagMedia($tag);
echo 'Max ID: ' . $media->pagination->next_max_id;
// Display first results
foreach ($media->data as $data) {
echo "<img src=\"{$data->images->thumbnail->url}\">";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.