|

|  How to Integrate Instagram Graph API to Access User Media in PHP

How to Integrate Instagram Graph API to Access User Media in PHP

October 31, 2024

Learn to integrate Instagram Graph API in PHP to access user media effortlessly. Follow our step-by-step guide to streamline your development process.

How to Integrate Instagram Graph API to Access User Media in PHP

 

Prerequisites and Environment

 

  • Ensure you have PHP installed on your server, ideally PHP 7.4 or higher, with cURL extension enabled.
  • Composer must be installed for managing dependencies.
  • Create a Facebook App and link it to Instagram for use with the Instagram Graph API. You'll need a valid Access Token with the required permissions.

 

Install Required Libraries

 

  • Use Composer to install the Facebook Graph SDK for PHP, which will facilitate API communication.

 

composer require facebook/graph-sdk

 

Authenticating with the Instagram Graph API

 

  • Obtain a User Access Token with the necessary permissions (e.g., `instagram_basic`, `instagram_manage_insights`, etc.).
  • Generate a Long-Lived Access Token if you require more extended access without frequent re-authorization.

 

Setting Up the PHP Environment

 

  • Set up your PHP script to use the Facebook Graph SDK to initialize the Graph API Client.
  • Use the Access Token obtained earlier to authorize requests.

 

require_once __DIR__ . '/vendor/autoload.php';

use Facebook\Facebook;

$fb = new Facebook([
  'app_id' => '{app-id}',
  'app_secret' => '{app-secret}',
  'default_graph_version' => 'v16.0',
]);

$accessToken = '{access-token}'; 

 

Fetching User Media

 

  • Use the `/me/media` endpoint to request a user's media data.
  • Build and execute a request to the Instagram Graph API to fetch media.

 

try {
  $response = $fb->get('/me/media?fields=id,caption,media_type,media_url,thumbnail_url,permalink,timestamp', $accessToken);
  $media = $response->getDecodedBody();

  foreach ($media['data'] as $mediaDatum) {
    echo 'ID: ' . $mediaDatum['id'] . '<br>';
    echo 'Caption: ' . $mediaDatum['caption'] . '<br>';
    echo 'Media Type: ' . $mediaDatum['media_type'] . '<br>';
    echo 'Media URL: ' . $mediaDatum['media_url'] . '<br>';
    echo 'Permalink: ' . $mediaDatum['permalink'] . '<br>';
    echo 'Timestamp: ' . $mediaDatum['timestamp'] . '<br><br>';
  }
} catch(Facebook\Exceptions\FacebookResponseException $e) {
  echo 'Graph returned an error: ' . $e->getMessage();
  exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
  echo 'Facebook SDK returned an error: ' . $e->getMessage();
  exit;
}

 

Handling Errors and Exceptions

 

  • Include error checking to handle possible exceptions when communicating with the API.
  • Implement loggers or alerts to keep track of errors for easier debugging and maintenance.

 

Refreshing Tokens

 

  • Keep track of the token expiry to refresh Access Tokens as needed using the Facebook API SDK methods.
  • Consider implementing a cron job for seamless token management.

 

Security Considerations

 

  • Never expose your `app secret` or `access token` publicly. Store them securely in environment variables or secure configuration files.
  • Regularly audit permissions and refresh tokens routinely to maintain security and access control.