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);
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()