I
I
Irina Travel2017-05-02 14:41:10
PHP
Irina Travel, 2017-05-02 14:41:10

PHP Instagram API: how to bypass the limit in the number of returned images?

Good day everyone! :D
I'm struggling with the Instagram API to bypass their limit when displaying images from my user's profile. By default, for some reason, this is the number 33. Why is it so - I didn’t find it in the docks ...
Here is a simple function to get these 33 images (I use saving the received response in a JSON file with subsequent parsing so as not to load traffic ):

function get_instagram($user_id = <MY_USER_ID>, $scope = 'public_content', $count = 100, $width = 190, $height = 190)
{

  $url = 'https://api.instagram.com/v1/users/' . $user_id . '/media/recent/?access_token=<MY_ACCESS_TOKEN>&count=' . $count . '&scope=' . $scope;

  $cache = './cache/' . sha1($url) . '.json';
  if (file_exists($cache) && filemtime($cache) > time() - 60 * 60){
    $jsonData = json_decode(file_get_contents($cache));
  } 
  else {
    // Получаю JSON с ответом из API и сохраняю 
    $jsonData = json_decode(file_get_contents($url));
    file_put_contents($cache,json_encode($jsonData));
  }

  $result = '<div id="instagram">';
  foreach ($jsonData->data as $key => $value) {
    $result .= '<img src="' . $value->images->low_resolution->url . '" alt="' . $value->caption->text . '" width="' . $width . '" height="' . $height . '" /></a>';
  }
  $result .= '</div>';

  return $result;
}

print get_instagram();

As I said earlier, all 33 pictures are displayed perfectly, but no matter how much I set the parameter $count, it doesn’t work anymore, it only works to a lesser extent :(
Maybe someone has encountered this problem and can help? Also, I will be grateful for the live cases (with code) to figure it out... it's already interesting for me!
ADDED
After wise thoughts from the comments, I added the merging of two objects to my code (which is higher pagination) next_url.
...
  }
  else {
    // Получаю JSON-ы с ответами из API
    $first_url = json_decode(file_get_contents($url));
    $next_url = json_decode(file_get_contents($first_url->pagination->next_url));
    
    // Сшиваю два объекта в один
    $jsonData = (object) array_merge_recursive((array) $first_url, (array) $next_url);

    // Сохраняю
    file_put_contents($cache, json_encode($jsonData));
  }
...

But here the question arises: well, this will work for the last 66 photos, but what if there are more than 500 of them? For each pagination step ( next_url) write your own handler, and then stitch the objects together? Maybe tell me a function (algorithm) how to simplify this task? :)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vit, 2017-05-02
@IrinaTravel

For one request more than 34 photos will not receive means. Use the MAX_ID and MIN_ID parameters to load data in batches.

T
Tom Nolane, 2017-05-02
@tomnolane

Vit - rightly noted, look at this:
github /mgp25/Instagram-API
maybe it will help you (ps I use it for posting myself)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question