Instagrammable Hell

Instagrammable Hell
Photo by Luke van Zyl / Unsplash

As I've spent a quantity of hours finding the right combination of PHP code to programmatically publish a video on my own account on Instagram, I write here some note for future myself and for people wandering online.

I've used this PHP package, provided by Meta and poorly documented (as everything, from Meta).

TL;DR the code solution is the following:

$api = \FacebookAds\Api::init($client_id, $client_secret, $token);
$api->getHttpClient()->setDefaultGraphBaseDomain('instagram.com');

$page = new \FacebookAds\Object\IGUser($page_id);

$media = $page->createMedia([], [
    'media_type' => 'REELS',
    'video_url' => 'https://example.com/my/own/video',
]);

sleep(20);

$page->createMediaPublish([], [
    'creation_id' => $media->id,
]);

The important parts:

  • the app on your own Meta's App Dashboard has to be enabled for Instagram Login and basic business functionalities, as described here
  • $client_id and $client_secret are the ones listed in the "Instagram > API setup with Instagram login" configuration panel of the Meta app, not the ones provided in the basic app settings
  • on the same page, it is possible to connect the target Instagram profile and obtain both account ID (to be used as $page_id) and $token
  • the alternative of the above mentioned instructions is to use the Facebook authentication, but this would involve far deeper configuration and integration between your Facebook account and your Instagram account. Something you may prefer to avoid, to skip all of the verifications and burocracy
  • the implication of using Instagram authentication is the need to change the main API's endpoint, using the setDefaultGraphBaseDomain() method of the internal HTTP client in the Meta's SDK
  • eventually the final sleep() between media upload and media publication may be replaced with some elegant implementation of webhooks, to be clearly notified when the content is actually ready to be pushed on your followers' feeds, but... Oh, c'mon...

Some more documentation about parameters to be passed to createMedia() and createMediaPublish() can be found in this page. At least, until they change again.

That's it. You're welcome.