Displaying all your Instagram images, ignoring next page and pagination

Sometimes you’d like to know exactly how many images a given Instagram account has since creation. This unfortunately is not an option with the current API and today I’ll discuss a work around.

Here’s an example of an Instagram response, note the “next_url” object.

[pagination] => stdClass Object
            [next_max_tag_id] => 1335219704338
            [deprecation_warning] => next_max_id and min_id are deprecated for this endpoint; use min_tag_id and max_tag_id instead
            [next_max_id] => 1335219704338
            [next_min_id] => 1335219773953
            [min_tag_id] => 1335219773953
            [next_url] => https://api.instagram.com/v1/tags/fun/media/recent?access_token=20576411.a30aaf6.c50198ecb09747788f0310e2c49964ed&min_id=&max_id=&max_tag_id=1335219704338

    [meta] => stdClass Object
             => 200

    [data] => Array
            [0] => stdClass Object

As you can see in the above example, The pagination object contains "next_url" which is a link to the next set of pictures. So what if you don't want a link to the next set but you want one big, bulk response of all the pictures? Where create an array of responses of course!

function __apiCall($url, $post_parameters = FALSE) {

    	// Initialize the cURL session
	    $curl_session = curl_init();

	    // Set the URL of api call
		curl_setopt($curl_session, CURLOPT_URL, $url);

		// If there are post fields add them to the call
		if($post_parameters !== FALSE) {
			curl_setopt ($curl_session, CURLOPT_POSTFIELDS, $post_parameters);

		// Return the curl results to a variable
	    curl_setopt($curl_session, CURLOPT_RETURNTRANSFER, 1);

	    // Execute the cURL session
	    $contents = curl_exec ($curl_session);

		// Close cURL session
		curl_close ($curl_session);

		// Return the response
		return  json_decode($contents);

As you can see in the above snippet (reading the comments) we take the set of parameters, use curl to send them to the endpoint, receive and decode our response and return it. Simple. What we need to do is leverage this function, with another function that will keep looping as long as it finds a "next_url" object in the currently received JSON response.

function getResults($url){

        $gotAllResults = false;
        $results = array();

        while(!$gotAllResults) {
        $result = $this->__apiCall($url);
        $results[] = $result;

        if (!property_exists($result->pagination, 'next_url')) {
            $gotAllResults = true;
        } else {
            $url = $result->pagination->next_url;

    return $results;


And there you have it. The function above takes the url (endpoint for the API), defines the $results variable as an empty array that we will add to for each page and the $gotAllResults variable, which will be set to TRUE once there are no more pages(or "next_url" objects). Our while statement first tests that $gotAllResults is FALSE then performs the _apiCall function seen further above, receives the response and adds it to the array. The if state checks the current response to see if the "next_url" object exists, if it no longer does(meaning we have reached the end of the responses) it will set $gotAllResponses to TRUE, cancel out the while statement and return our array of results.

Now that we have our big array of JSON responses, parsing it can be tricky. Below is a snippet similar to what I uses to parse and display my API call, showing the image and some information about it...