A lightweight PHP library to fetch and display Instagram media feeds using the Instagram Graph API.
- 📸 Fetch Instagram posts and media
- 🖌️ Display images with captions
- ⚡ Pure PHP implementation (no framework required)
- 🔒 Secure Instagram Graph API integration
- 📦 Easy to integrate into existing projects
- 🛠️ Handles edge cases (missing captions, null values)
See the live demo: https://instagram-feed-php.herokuapp.com/
- PHP >= 7.2
- cURL extension enabled
- Instagram Graph API Access Token
# Clone the repository
git clone https://github.com/sakibian/instagram-feed-php.git
cd instagram-feed-php-
Get your Instagram Graph API access token from Facebook Developers
-
Update the
$tokenvariable in the PHP file:
$token = 'YOUR_INSTAGRAM_ACCESS_TOKEN';<?php
include 'instagram-feed.php';
// Fetch and display Instagram feed
$feed = getInstagramFeed($token);
foreach ($feed as $post) {
echo '<img src="' . $post['media_url'] . '" alt="' . $post['caption'] . '">';
}
?>This implementation includes a fix for the "undefined index caption" error that occurs when Instagram posts have no captions. The solution is inspired by the Dymantic Laravel Instagram Feed MediaParser implementation.
// Caption handling
$caption = isset($post['caption']) ? $post['caption'] : '';This project is based on php-curl-instagram-graph by @ribeiroeder, with enhancements for handling null captions and improved error handling.
The library uses Instagram Graph API to fetch user media:
GET https://graph.instagram.com/me/media
id- Media IDmedia_type- Type of media (IMAGE, VIDEO, CAROUSEL_ALBUM)media_url- URL of the media filecaption- Post caption texttimestamp- Publication date
Instagram access tokens expire after 60 days. You'll need to generate a new token when it expires.
If you encounter "undefined index caption" errors, ensure you're using the latest version of this library which includes the null caption handling fix.
MIT
Contributions are welcome! Please feel free to submit a Pull Request.