PHP code example of amirsarhang / instagram-php-sdk

1. Go to this page and download the library: Download amirsarhang/instagram-php-sdk 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/ */

    

amirsarhang / instagram-php-sdk example snippets


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

use Amirsarhang\Instagram;
...
public function callback()
{
    // Generate User Access Token After User Callback To Your Site
    return Instagram::getUserAccessToken();
}

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

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"]);
}

use Amirsarhang\Instagram;
...
public function userInfo()
{

    $instagram = new Instagram($access_token);

    $endpoint = '/me?fields=id,name';

    return $instagram->get($endpoint);

}

// 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;

return $instagram->addComment($recipient_id, 'Test Reply');

return $instagram->deleteComment($comment_id);

return $instagram->hideComment($comment_id, true); // false for UnHide

// 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;

return $instagram->addTextMessage($recipient_id, 'Test DM');

return $instagram->addMediaMessage($recipient_id, '<IMAGE_URL>');
bash
composer 
bash
"  "amirsarhang/instagram-php-sdk": "2.0.0"
},