PHP code example of texhub / instagram-graph-api

1. Go to this page and download the library: Download texhub/instagram-graph-api library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

texhub / instagram-graph-api example snippets


use TexHub\InstagramGraphApi\Instagram;

$ig = Instagram::make('APP_ID', 'APP_SECRET');

// a) Send the user to authorize:
$url = $ig->oauth()->authorizationUrl(
    scopes: ['instagram_business_basic', 'instagram_business_content_publish',
             'instagram_business_manage_comments', 'instagram_business_manage_messages'],
    state: 'csrf-token',
    redirectUri: 'https://shop.tj/instagram/callback',
);
// redirect($url)

// b) On the callback, exchange the ?code= for tokens:
$short = $ig->oauth()->requestShortLivedToken($_GET['code'], 'https://shop.tj/instagram/callback');
$long  = $ig->oauth()->exchangeForLongLivedToken($short->token); // valid ~60 days

echo $long->token;
echo $long->expiresAt();        // unix timestamp
$long->expiresWithinDays(7);    // refresh soon?

// c) Refresh before it expires:
$refreshed = $ig->oauth()->refreshLongLivedToken($long->token);

$ig = Instagram::make('APP_ID', 'APP_SECRET', accessToken: $long->token, igUserId: '17841...');
// or, from an existing instance:
$ig = $ig->withAccessToken($long->token);

$me = $ig->users()->me();
$me->get('username');
$me->get('followers_count');

$ig->users()->avatarUrl();   // profile picture URL
$ig->users()->username();

// Photo (create container + publish in one call):
$ig->media()->publishPhoto('https://cdn.shop.tj/photo.jpg', 'Новинка! 🔥');

// Reel:
$ig->media()->publishReel('https://cdn.shop.tj/reel.mp4', 'Смотрите 👀');

// Story:
$ig->media()->publishStory('https://cdn.shop.tj/story.jpg');

// Carousel (multi-image post):
$a = $ig->media()->createCarouselItem('https://cdn/1.jpg')->id();
$b = $ig->media()->createCarouselItem('https://cdn/2.jpg')->id();
$carousel = $ig->media()->createCarousel([$a, $b], 'Подборка')->id();
$ig->media()->publish($carousel);

// Read:
$ig->media()->list();
$ig->media()->get($mediaId);
$ig->media()->publishingLimit();   // daily quota usage

$ig->comments()->forMedia($mediaId);             // list comments
$ig->comments()->commentOnMedia($mediaId, 'Спасибо за внимание!');
$ig->comments()->reply($commentId, 'Ответ на комментарий');
$ig->comments()->hide($commentId);               // hide / unhide
$ig->comments()->delete($commentId);

use TexHub\InstagramGraphApi\Builders\Button;

$ig->messages()->sendText($igsid, 'Привет! Чем помочь?');
$ig->messages()->sendImage($igsid, 'https://cdn/promo.jpg');

// Buttons:
$ig->messages()->sendButtons($igsid, 'Выберите действие:', [
    Button::url('Открыть сайт', 'https://texhub.pro'),
    Button::postback('Связаться', 'CONTACT'),
]);

// Quick replies:
$ig->messages()->sendQuickReplies($igsid, 'Ваш выбор?', [
    Button::quickReply('Да', 'YES'),
    Button::quickReply('Нет', 'NO'),
]);

// Typing indicator / read receipts / reactions:
$ig->messages()->senderAction($igsid, 'typing_on');
$ig->messages()->react($igsid, $messageId, 'love');

// Conversations & history:
$ig->messages()->conversations();
$ig->messages()->messages($conversationId);

$challenge = $ig->webhooks()->verifyChallenge($_GET);
if ($challenge !== null) { echo $challenge; exit; } // HTTP 200

$raw = file_get_contents('php://input');
$ig->webhooks()->assertValidSignature($raw, $_SERVER['HTTP_X_HUB_SIGNATURE_256'] ?? null);

foreach ($ig->webhooks()->parse($raw) as $event) {
    if ($event->isMessage()) {
        $ig->messages()->sendText($event->senderId(), 'Получили: ' . $event->messageText());
    }
    if ($event->isComment()) {
        // $event->get('id'), $event->get('text'), ...
    }
}
http_response_code(200);

use TexHub\InstagramGraphApi\Exceptions\ApiException;

try {
    $ig->media()->publishPhoto($url, $caption);
} catch (ApiException $e) {
    $e->httpStatus; $e->errorCode; $e->errorType; $e->errorSubcode; $e->fbtraceId;
    $e->isTokenError();   // code 190 — token expired/invalid
    $e->isRateLimit();
}

$ig->http()->get('17841.../insights', ['metric' => 'impressions,reach']);
$ig->http()->post($mediaId, ['comment_enabled' => 'false']);

use TexHub\InstagramGraphApi\Laravel\Instagram;

Instagram::media()->publishPhoto($url, 'Привет из Laravel!');
Instagram::messages()->sendText($igsid, 'Ответ');

public function verify(Request $request) {
    return response(Instagram::webhooks()->verifyChallenge($request->query()) ?? '', 200);
}

public function handle(Request $request) {
    Instagram::webhooks()->assertValidSignature(
        $request->getContent(),
        $request->header('X-Hub-Signature-256'),
    );

    foreach (Instagram::webhooks()->parse($request->getContent()) as $event) {
        // ...
    }
    return response('', 200);
}

// Onboarding: each tenant runs the OAuth flow → store their long-lived token.
$long = $ig->oauth()->exchangeForLongLivedToken($short->token);
// → save {token, user_id} for this tenant

// Act as any tenant — bind a client to their stored token:
$ig->withAccessToken($tenant->ig_token)->media()->publishPhoto($url, $caption);

// One webhook for everyone — route by the account that received it:
foreach ($ig->webhooks()->parse($raw) as $event) {
    $tenant = Tenant::where('ig_account_id', $event->accountId())->first();
    if ($event->isMessage()) { /* $event->senderId(), $event->messageText() */ }
}

use TexHub\InstagramGraphApi\Instagram;
use TexHub\InstagramGraphApi\Config;
use TexHub\InstagramGraphApi\Tests\Support\FakeTransport;

$t = (new FakeTransport())->push(['id' => 'MEDIA_1']);
$ig = new Instagram(new Config('APP', 'SECRET', accessToken: 'TOKEN'), $t);
// assert on $t->history / $t->lastUrl()
bash
php artisan vendor:publish --tag=instagram-config