Skip to content

Instantly share code, notes, and snippets.

@cosenary
Last active July 30, 2018 07:43
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save cosenary/1711218 to your computer and use it in GitHub Desktop.
Save cosenary/1711218 to your computer and use it in GitHub Desktop.
Instagram PHP API - How to get the most recent media published by an Instagram user.
<?php
/**
* Instagram PHP API
* Example for using the getUserMedia() method
*
* @link https://github.com/cosenary/Instagram-PHP-API
* @author Christian Metz
* @since 31.01.2012
*/
require 'Instagram.php';
use MetzWeb\Instagram\Instagram;
// Initialize class
$instagram = new Instagram(array(
'apiKey' => 'YOUR_APP_KEY',
'apiSecret' => 'YOUR_APP_SECRET',
'apiCallback' => 'YOUR_APP_CALLBACK'
));
// Receive OAuth code parameter
$code = $_GET['code'];
// Check whether the user has granted access
if (true === isset($code)) {
// Receive OAuth token object
$data = $instagram->getOAuthToken($code);
// Store user access token
$instagram->setAccessToken($data);
// Now you can call all authenticated user methods
// Get the most recent media published by a user
$media = $instagram->getUserMedia();
foreach ($media->data as $entry) {
echo "<img src=\"{$entry->images->thumbnail->url}\">";
}
}
?>
@abrad1212
Copy link

Its not working, photos not appearing!

@velrino
Copy link

velrino commented Jun 18, 2016

Its not working, photos not appearing!

@ernestohegi
Copy link

It seems you need to specify the limit of returned results explicitly.

This snippet worked for me:

$instagram->getUserMedia('self', 10)

@zvaehn
Copy link

zvaehn commented Mar 16, 2017

can i iterate this function as well?

@wazowski78
Copy link

This snippet seems to worked for me too:

$instagram->getUserMedia('self', 10)

But the popular feed is broken, any fix for that?

@kiranpwt
Copy link

kiranpwt commented Jun 13, 2018

Try!!! This worked for me!!

echo "<img src=\"{$entry->images->standard_resolution->url}\">";

@axsddlr
Copy link

axsddlr commented Jul 30, 2018

how can i run this file on localhost?

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