Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

commented Jun 5, 2016

Its not working, photos not appearing!

@velrino

This comment has been minimized.

Copy link

commented Jun 18, 2016

Its not working, photos not appearing!

@ernestohegi

This comment has been minimized.

Copy link

commented Aug 25, 2016

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

This snippet worked for me:

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

@zvaehn

This comment has been minimized.

Copy link

commented Mar 16, 2017

can i iterate this function as well?

@wazowski78

This comment has been minimized.

Copy link

commented Jun 19, 2017

This snippet seems to worked for me too:

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

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

@kiranpwt

This comment has been minimized.

Copy link

commented Jun 13, 2018

Try!!! This worked for me!!

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

This comment has been minimized.

Copy link

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
You can’t perform that action at this time.