1

I've got this working to pull recent Instagram posts into Wordpress, however the "recent" photos that are displayed aren't actually my most recent Instagram posts. When I refresh, the Instagram posts that are pulled in also change. I can only find a max/min time within the API docs. Is there any way to just get the last 3 Instagram posts made?

<?php
$json = file_get_contents('https://api.instagram.com/v1/users/user-id/media/recent/?access_token=key-number');
$a_json = json_decode($json, true);
foreach( $a_json['data'] as $key => $value) {
  $a_images[$value['id']]['link'] = $value['link'];
  $a_images[$value['id']]['url'] = $value['images']['standard_resolution']['url'];
  $a_images[$value['id']]['caption'] = $value['caption']['text'];
}
shuffle($a_images);

echo '<ul>';
$i = 0;
foreach($a_images as $image) {
  if ($i < 3) {
    echo '<li class="large-4 columns">
        <div class="front">
            <a href="'.$image[link].'" target="_blank">
                <img src="'.$image[url].'"/>
            </a>
        </div>

        <div class="back">
            <p>'.$image[caption].'</p>
        </div>
    </li>';
    $i++;
  }
}
echo '</ul>';
brasofilo
  • 25,496
  • 15
  • 91
  • 179
SeekingCharlie
  • 607
  • 2
  • 8
  • 19
  • The API has an end point [`/user/media/recent/`](http://instagram.com/developer/endpoints/users/), ain't this return a proper list? I don't have an Instagram account to test it, but [check this implementation of the Flicker API](http://stackoverflow.com/a/13638937/1287812). – brasofilo Oct 20 '13 at 14:33
  • It's returning a list of recent photos, but it's not ordered by the most recent (hope that's clear). – SeekingCharlie Oct 21 '13 at 00:08
  • Yes, it is. Sorry, I misread the code the first time, you're already using this end point. – brasofilo Oct 21 '13 at 00:09

1 Answers1

1

You are receiving the data in the right order, but then you are using the shuffle() function on the images array $a_images you created .

if you remove the shuffle($a_images); from your code it'll be just fine.

and replace $image[link], $image[url] and $image[caption] with $image['link'], $image['url'] and $image['caption']

sorry for bad english

Marcos Rodrigues
  • 712
  • 6
  • 10