使用Facebook Graph API发布Instagram视频/混合轮播帖问题求助
Instagram图文/纯视频轮播帖发布问题求助
已通过Facebook Graph API实现应用内Instagram内容自动发布功能,目前单图、单视频、Reels、故事及图片轮播帖的发布均正常,但在发布纯视频轮播帖或图文混合轮播帖时遇到问题:多数情况下第一个视频/图片的容器创建成功,但第二个媒体容器常出现超时、Graph返回无明确信息的错误或创建失败。以下是我的PHP代码,恳请提供解决方案:
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(); }
已查阅官方文档及网络资料,但仍未找到解决办法!
内容的提问来源于stack exchange,提问作者Atendimento Agência Prime
相关产品推荐
相关产品推荐

