Skip to content
Advertisement

Instagram Basic Display API: how can I limit the number of posts to less than 20?

Using the Instagram Basic Display API, how can I limit the number of posts that it fetches to less than 20?

Inside a class in PHP, I defined the following base URL for the endpoints:

private $_apiBaseUrl = 'https://api.instagram.com/';
private $_graphBaseUrl = 'https://graph.instagram.com/';
public $userId = '';  // defined here, fetched later by getUser();

These two functions get the posts:

 // get the posts
        public function getUsersMedia() {
            $params = array(
                'endpoint_url' => $this->_graphBaseUrl . $this->userId . '/media',
                'type' => 'GET',
                'url_params' => array(
                    'fields' => 'id,caption,media_type,media_url',
                )
            );

            $response = $this->_makeApiCall( $params );
            return $response;
        }


   // get more info on a specific post
        public function getMedia( $mediaId ) {
            $params = array(
                'endpoint_url' => $this->_graphBaseUrl . $mediaId,
                'type' => 'GET',
                'url_params' => array(
                    'fields' => 'id,caption,media_type,media_url,permalink,thumbnail_url,timestamp,username'
                )
            );

            $response = $this->_makeApiCall( $params );
            return $response;
        }

In this post I found this endpoint:

https://graph.instagram.com/{user-id}/media?fields={media-fields-you-want-to-return}&access_token={access-token}&limit={number-of-media-you-want-to-return}

But how would I make this work with my functions? I don’t want to use pagination, but only return like, say, the last 5-10 posts.

Advertisement

Answer

Add an entry for the limit parameter to the url_params arrays:

'url_params' => array(
   'fields' => 'id,caption,media_type,media_url',
   'limit'  => 5,
)
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement