Hey guys so im trying to automate the post on intagram in my app using the api graph from facebook, i already got the single post of pictures and videos of reels, stories, and post, got as well a carousel of pictures but im having a lot of problem trying to make works to POST a carousel that contais or just reels (videos) and mixed onde having reels and pictures, most of the time i pass the process of creating the container for the first video or picture (when mixed) but in the second one usully or i get a timeout or a erro from graph that dont specify nothing or it fails to create the 2 container and pass the status finished, if anyone can help me would be much appreciated ! my code below !

 function postOnInstagram($fb, $message, $mediaUrls = [], $isCarrosel = false, $pageAccessToken, $instagramAccountId, $hasVideo = false, $typePost){   $fb->setDefaultAccessToken($pageAccessToken);   try {       switch ($typePost) {           case 'post':               if ($hasVideo) {                   return $isCarrosel                        ? postInstagramMediaCarousel($fb, $instagramAccountId, $message, $mediaUrls)                       : postInstagramSingleVideo($fb, $instagramAccountId, $message, $mediaUrls[0]);               } else {                   return $isCarrosel                        ? postInstagramPhotoCarousel($fb, $instagramAccountId, $message, $mediaUrls)                       : postInstagramSinglePhoto($fb, $instagramAccountId, $message, $mediaUrls[0]);               }           case 'reel':               if (!$hasVideo) {                   throw new Exception("Reels require a video");               }               if ($mediaUrls > 1) {                 return postInstagramMultipleVideos($fb, $instagramAccountId, $message, $mediaUrls);               }               else {                 return postInstagramReel($fb, $instagramAccountId, $message, $mediaUrls[0]);               }           case 'story':               if ($hasVideo) {                   return postInstagramStoryVideo($fb, $instagramAccountId, $message, $mediaUrls[0]);               } elseif (!empty($mediaUrls)) {                   return postInstagramStoryPhoto($fb, $instagramAccountId, $message, $mediaUrls[0]);               } else {                   return postInstagramStoryText($fb, $instagramAccountId, $message);               }           default:               throw new Exception("Invalid post type");       }   } catch (FacebookResponseException $e) {       return 'Graph returned an error: ' . $e->getMessage();   } catch (FacebookSDKException $e) {       return 'Facebook SDK returned an error: ' . $e->getMessage();   } catch (Exception $e) {       return 'General error: ' . $e->getMessage();   } } function postInstagramMediaCarousel($fb, $instagramAccountId, $message, $mediaUrls){     $mediaIds = [];     foreach ($mediaUrls as $mediaUrl) {         sleep(10);         // Detect media type based on URL or other means         $mediaType = (preg_match('/\.(mp4)$/i', $mediaUrl)) ? 'VIDEO' : 'IMAGE';         // Create the media and get the creation ID         $creationId = createMediaInsta($fb, $instagramAccountId, $mediaUrl, $mediaType);         if (!$creationId) {             throw new Exception("Failed to create media: $mediaUrl");         }         // Wait for the media to be ready         if (!waitForMediaToBeReadyInsta($fb, $creationId)) {             throw new Exception("Media is not ready: $creationId");         }         $mediaIds[] = $creationId;     }         // Create carousel with the obtained media IDs     $carouselResponse = $fb->post("/$instagramAccountId/media", [         'caption' => $message,         'media_type' => 'CAROUSEL',         'children' => $mediaIds,     ]);       $carouselCreationId = $carouselResponse->getDecodedBody()['id'];     if (!$carouselCreationId) {         throw new Exception("Failed to create carousel.");     }     // Wait for the carousel to be ready     if (!waitForMediaToBeReadyInsta($fb, $carouselCreationId)) {         throw new Exception("Carousel is not ready: $carouselCreationId");     }     sleep(30);     // Publish the carousel     return publishMedia($fb, $instagramAccountId, $carouselCreationId); } function createMediaInsta($fb, $instagramAccountId, $mediaUrl, $mediaType){     // Configura o payload dependendo do tipo de mídia     $payload = [         'media_type' => $mediaType,         'is_carousel_item' => 'true'     ];     if ($mediaType === 'VIDEO') {         $payload['video_url'] = $mediaUrl;     } else {         $payload['image_url'] = $mediaUrl;     }     $response = $fb->post("/$instagramAccountId/media", $payload);     return $response->getDecodedBody()['id']; } function waitForMediaToBeReadyInsta($fb, $creationId, $timeout = 300, $interval = 10){     $start = time();     do {         sleep($interval);         try {             $response = $fb->get("/$creationId");             $status = $response->getDecodedBody()['status'];         } catch (Exception $e) {             // Log the exception and continue waiting             error_log("Exception: " . $e->getMessage());             return false;         }         if ($status === 'READY') {             return true;         }         if (time() - $start > $timeout) {             error_log("Timeout: Media not ready in time.");             return false;         }     } while (true); } function publishMedia($fb, $instagramAccountId, $creationId){   return $fb->post("/$instagramAccountId/media_publish", [       'creation_id' => $creationId   ])->getDecodedBody(); } 

i tried everything that i could find on docs and net but cant find a solution!

Tag:php, facebook-graph-api, instagram-api, instagram-graph-api

2 comments.

  1. Collin Allaert

    It seems that everyone is experiencing the same issue, so the problem is likely on Instagram's end, not yours.

    https://developers.facebook.com/community/threads/1270072723978459/

    There are a lot of threads about all the instagram bugs right now because access_token was also not working but its been fixed.

    1. Atendimento Agência Prime

      i will keep a eye on that thread!

Add a new comment.