Skip to content

Instantly share code, notes, and snippets.

@cosenary
Last active June 21, 2020 16:56
Show Gist options
  • Save cosenary/2959827 to your computer and use it in GitHub Desktop.
Save cosenary/2959827 to your computer and use it in GitHub Desktop.
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}\">";
}
@Farru
Copy link

Farru commented Jun 21, 2020

fsdfsd

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment