Instagram API: How to get all user media?

后端 未结 10 2100
灰色年华
灰色年华 2020-11-28 02:36

In general I need to get all user media.

User has more than 250 photos.

I do /users/1/media/recent/?access_token=...&count=250

But i

10条回答
  •  天命终不由人
    2020-11-28 03:10

    Instagram developer console has provided the solution for it. https://www.instagram.com/developer/endpoints/

    To use this in PHP, here is the code snippet,

    /**
    **
    ** Add this code snippet after your first curl call
    ** assume the response of the first call is stored in $userdata
    ** $access_token have your access token
    */
    
    $maximumNumberOfPost = 33; // it can be 20, depends on your instagram application
    $no_of_images = 50 // Enter the number of images you want
    
    if ($no_of_images > $maximumNumberOfPost) {
    
        $ImageArray = [];
        $next_url = $userdata->pagination->next_url;
        while ($no_of_images > $maximumNumberOfPost) {
               $originalNumbersOfImage = $no_of_images;
               $no_of_images = $no_of_images - $maximumNumberOfPost;
               $next_url = str_replace("count=" . $originalNumbersOfImage, "count=" . $no_of_images, $next_url);
               $chRepeat = curl_init();
               curl_setopt_array($chRepeat, [
                                 CURLOPT_URL => $next_url,
                                 CURLOPT_HTTPHEADER => [
                                        "Authorization: Bearer $access_token"
                                  ],
                                  CURLOPT_RETURNTRANSFER => true
                                ]);
                $userRepeatdata = curl_exec($chRepeat);
                curl_close($chRepeat);
                if ($userRepeatdata) {
                          $userRepeatdata = json_decode($userRepeatdata);
                          $next_url = $userRepeatdata->pagination->next_url;
                         if (isset($userRepeatdata->data) && $userRepeatdata->data) {
                              $ImageArray = $userRepeatdata->data;
                       }
               }
        }
    
    }
    

提交回复
热议问题