amirsarhang / instagram-php-sdk
It's Instagram Graph SDK for PHP. With this package you can easily make all requests to Instagram Graph API, like Auth and CRUD. Also, we will have more methods regularly.
Installs: 6 583
Dependents: 0
Suggesters: 0
Security: 0
Stars: 43
Watchers: 3
Forks: 6
Open Issues: 0
Requires
- php: >=8.0
- nickdnk/graph-sdk: ^7.0.1
- vlucas/phpdotenv: ^5.3
README
It's Instagram Graph SDK for PHP.
With this package you can easily make all requests to Instagram Graph API, like Auth and CRUD. Also, we will have more methods regularly.
This project adheres to a Contributor Code of Conduct. By participating in this project and its community, you are expected to uphold this code.
Installation
The preferred method of installation is via Composer. Run the following
command to install the package and add it as a requirement to your project's
composer.json
:
composer require amirsarhang/instagram-php-sdk
Or add the following to your composer.json file:
"require": { "amirsarhang/instagram-php-sdk": "2.0.0" },
Documentation
Requirements
*Please remember that you need verified Facebook APP to use this sdk.
FACEBOOK_APP_ID
FACEBOOK_APP_SECRET
Configuration
Put these values in your .env file:
FACEBOOK_APP_ID="<YOUR_FACEBOOK_APP_ID>" // Get it from your FB developer dashboard FACEBOOK_APP_SECRET="<YOUR_FACEBOOK_APP_SECRET>" // Get it from your FB developer dashboard FACEBOOK_GRAPH_VERSION="v10.0" // Your Graph version >= v10.0 INSTAGRAM_CALLBACK_URL="https://yoursite.com/instagram/callback" // Instagram callback after login
Auth & Login
use Amirsarhang\Instagram; ... public function login() { // Go to FB Documentations to see available permissions $permissions = [ 'instagram_basic', 'pages_show_list', 'instagram_manage_comments', 'instagram_manage_messages', 'pages_manage_engagement', 'pages_read_engagement', 'pages_manage_metadata' ]; // Generate Instagram Graph Login URL $login = (new Instagram())->getLoginUrl($permissions); // Redirect To Facebook Login & Select Account Page return header("Location: ".$login); }
- Please remember that your added permissions need verified by Facebook.
Here you can find Facebook Permissions.
Generate & Save User Access Token in your Database.
use Amirsarhang\Instagram; ... public function callback() { // Generate User Access Token After User Callback To Your Site return Instagram::getUserAccessToken(); }
Then we are ready to generate our page access token, but first we should get all selected page by user, then show them to your user to select which page's access token should be save in Database.
use Amirsarhang\Instagram; ... public function instagramAccounts(): array { $token = "<USER_ACCESS_TOKEN>"; // We got it in callback $instagram = new Instagram($token); // Will return all instagram accounts that connected to your facebook selected pages. return $instagram->getConnectedAccountsList(); }
Sample Response
[
"success": "true",
"instagramAccounts": [
{
"name": "Test Page",
"biography": "This is Test account",
"username": "username",
"followers_count": 167000,
"follows_count": 1,
"media_count": 231,
"profile_picture_url": "https://scontent.fist6-1.fna.fbcdn.net/v/123.jpg?_nc_cat=109&ccb=1-5&_nc_sid=86c713&_nc_ohc=Gnf5d6wdF3UAX8jVBUl&_nc_ht=scontent.fist6-1",
"id": "17841111111111111",
"fb_page_id": "108011111111111",
"fb_page_access_token": "EAAWq8obOe68BAA1r8GUHqOvVZHDY&WFWKHBDfJrjPswLuMb4E8ZCxCRvNW37bt9tslaBBRbTv"
},
{
"name": "Test Page2",
"biography": "This is other Test account",
"username": "username2",
"followers_count": 1200,
"follows_count": 22,
"media_count": 23,
"profile_picture_url": "https://scontent.fist6-1.fna.fbcdn.net/v/456.jpg?_nc_cat=109&ccb=1-5&_nc_sid=86c713&_nc_ohc=Gnf5d6wdF3UAX8jVBUl&_nc_ht=scontent.fist6-1",
"id": "17841222222222222",
"fb_page_id": "108022222222222",
"fb_page_access_token": "XXREWDY&WFWKHBDfJrjPswLuMb4E8ZCxCRvNW37bt9tslBCFEZDZD"
}]
]
After storing selected page data by user in your database, then you need to call subscribeWebhook()
to register this page for get real time Events.
use Amirsarhang\Instagram; ... public function registerWebhook() { $token = "<FACEBOOK_PAGE_ACCESS_TOKEN>"; $fb_page_id= "<FACEBOOK_PAGE_ID>"; $instagram = new Instagram($token); // Default subscribe with email field return $instagram->subscribeWebhook($fb_page_id, $token); // You can pass your needed fields as an Array in the last parameter. // Your app does not receive notifications for changes to a field // unless you configure Page subscriptions in the App Dashboard and subscribe to that field. return $instagram->subscribeWebhook($fb_page_id, $token, ["email", "feed", "mentions"]); }
Check this link for more details about page subscriptions.
Usage
use Amirsarhang\Instagram; ... public function userInfo() { $instagram = new Instagram($access_token); $endpoint = '/me?fields=id,name'; return $instagram->get($endpoint); }
- If your request is on graphEdge, you can pass
true
on$instagram->get($endpoint, true)
as second parameter.
Methods
Comment Methods
Get Comment Data
// Get default Comment fields data (Timestamp, text, id) $get_comment = $instagram->getComment($comment_id); // If you need other fields you can send them as array $get_comment = $instagram->getComment($comment_id, ['media','like_count']); return $get_comment;
Add Comment
return $instagram->addComment($recipient_id, 'Test Reply');
Delete Comment
return $instagram->deleteComment($comment_id);
Hide & UnHide Comment
return $instagram->hideComment($comment_id, true); // false for UnHide
Messaging Methods
Get Message Data
// Get default Message fields data (message, from, created_time, attachments, id) $get_message = $instagram->getMessage($message_id); // If you need other fields you can send them as array $get_message = $instagram->getMessage($message_id, ['attachments','from']); return $get_message;
Send Text Message (Direct Message)
return $instagram->addTextMessage($recipient_id, 'Test DM');
Send Media Message (Direct Message)
return $instagram->addMediaMessage($recipient_id, '<IMAGE_URL>');
I will add more Useful methods as soon as possible :)
Check out the documentation website for detailed information and code examples.
Contributing
Contributions are welcome! Please read CONTRIBUTING for details.
Copyright and License
The amirsarhang/instagram-php-sdk library is copyright © Amirhossein Sarhangian and licensed for use under the MIT License (MIT). Please see LICENSE for more information.