1. Go to this page and download the library: Download mkhab7/php-instagram-graph 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/ */
mkhab7 / php-instagram-graph example snippets
use Mkhab7\InstagramGraph\InstagramGraph;
use Mkhab7\InstagramGraph\Auth\AccessToken;
// Initialize client for authentication
$instagram = InstagramGraph::forAuthentication(
appId: $_ENV['INSTAGRAM_APP_ID'],
appSecret: $_ENV['INSTAGRAM_APP_SECRET'],
redirectUri: $_ENV['INSTAGRAM_REDIRECT_URI']
);
// Step 1: Generate authorization URL
$authUrl = $instagram->auth()->getAuthorizationUrl(
scopes: ['instagram_business_basic', 'instagram_business_manage_messages'],
state: 'random_state_string',
forceReauth: true
);
// Redirect user to $authUrl
// Step 2: Handle callback and exchange code for token
$code = $_GET['code']; // From callback
$accessToken = $instagram->auth()->completeOAuthFlow($code);
// Step 3: Use authenticated client
$authenticatedInstagram = InstagramGraph::withAccessToken(
appId: $_ENV['INSTAGRAM_APP_ID'],
appSecret: $_ENV['INSTAGRAM_APP_SECRET'],
redirectUri: $_ENV['INSTAGRAM_REDIRECT_URI'],
accessToken: $accessToken
);
// Get current user information
$user = $authenticatedInstagram->users()->me();
echo "Welcome {$user->getDisplayName()}!\n";
echo "Account Type: {$user->accountType->getLabel()}\n";
echo "Followers: {$user->followersCount}\n";
echo "Media Count: {$user->mediaCount}\n";
// Get user's media
$mediaResponse = $authenticatedInstagram->users()->getUserMedia(limit: 10);
foreach ($mediaResponse['data'] as $mediaData) {
$media = \Mkhab7\InstagramGraph\Data\Media::fromArray($mediaData);
echo "Media: {$media->id}\n";
echo "Type: {$media->mediaType->getLabel()}\n";
echo "Likes: {$media->likesCount}\n";
echo "Comments: {$media->commentsCount}\n";
echo "---\n";
}
$userService = $instagram->users();
// Get current user with custom fields
$user = $userService->me([
'id', 'username', 'account_type', 'name', 'biography',
'website', 'followers_count', 'media_count'
]);
// Get another user by ID
$otherUser = $userService->getUser('1234567890');
// Business discovery (public account data)
$publicData = $userService->getBusinessDiscovery('username');
// Check if user exists
if ($userService->userExists('1234567890')) {
echo "User exists!\n";
}
// Get user insights (business accounts only)
$insights = $userService->getAccountInsights(
metrics: ['impressions', 'reach', 'profile_views'],
period: 'day',
since: new DateTime('-30 days'),
until: new DateTime('now')
);
$mediaService = $instagram->media();
// Get media by ID
$media = $mediaService->getMedia('media_id_here');
echo "Caption: {$media->caption}\n";
echo "Type: {$media->mediaType->getLabel()}\n";
echo "Posted: {$media->timestamp->format('Y-m-d H:i:s')}\n";
// Get media insights
$insights = $mediaService->getMediaInsights(
mediaId: 'media_id_here',
metrics: ['impressions', 'reach', 'engagement', 'saves']
);
// Create media container for publishing
$container = $mediaService->createMediaContainer(
imageUrl: 'https://example.com/image.jpg',
caption: 'Check out this amazing photo! #photography #art',
userTags: [
['user_id' => '1234567890', 'x' => 0.5, 'y' => 0.3]
]
);
// Publish the media
$published = $mediaService->publishMedia($container['id']);
// Create carousel post
$childContainers = [
$mediaService->createMediaContainer(
imageUrl: 'https://example.com/image1.jpg',
isCarouselItem: true
)['id'],
$mediaService->createMediaContainer(
imageUrl: 'https://example.com/image2.jpg',
isCarouselItem: true
)['id']
];
$carouselContainer = $mediaService->createCarouselContainer(
children: $childContainers,
caption: 'Swipe to see more! 👉'
);
$publishedCarousel = $mediaService->publishMedia($carouselContainer['id']);
// Search hashtags
$hashtags = $mediaService->searchHashtags('travel', limit: 10);
// Get hashtag top media
$topMedia = $mediaService->getHashtagTopMedia('hashtag_id_here');