Skip to content

Instantly share code, notes, and snippets.

@faiyazalam
Created January 3, 2017 17:46
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save faiyazalam/827a109eb6e365640c388224e1d51a31 to your computer and use it in GitHub Desktop.
Save faiyazalam/827a109eb6e365640c388224e1d51a31 to your computer and use it in GitHub Desktop.
Get recent images from Instagram using PHP & cURL
<?php
/**
* Supply a user id and an access token
* Jelled explains how to obtain a user id and access token in the link provided
* @link http://jelled.com/instagram/access-token
*/
$userid = "";
$accessToken = "";
// Get our data
function fetchData($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
// Pull and parse data.
$result = fetchData("https://api.instagram.com/v1/users/{$userid}/media/recent/?access_token={$accessToken}");
$result = json_decode($result);
?>
<?php $limit = 8; // Amount of images to show ?>
<?php $i = 0; ?>
<?php foreach ($result->data as $post): ?>
<?php if ($i < $limit ): ?>
<a href="<?= $post->images->standard_resolution->url ?>"><img src="<?= $post->images->standard_resolution->url ?>" width="500" height="500"></a>
<?php $i ++; ?>
<?php endif; ?>
<?php endforeach; ?>
@Selvasan02
Copy link

I am getting error like

Notice: Trying to get property of non-object in C:\xampp\htdocs\raj\insta_image.php on line 36

Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\raj\insta_image.php on line 36

@porp
Copy link

porp commented Jan 22, 2020

access token is terminated by instagram. so the code is not working anymore...

@ribeiroeder
Copy link

Script made based on the new (2020) Instagram API -> https://github.com/ribeiroeder/php-curl-instagram-graph

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