beloop/instagram
Read-only Instagram component from the Beloop LMS suite. Provides Instagram-related integration as part of beloop/components, built on Symfony and released under the MIT license. For support, issues, and PRs, use the main beloop/components repository.
Install the Package
composer require beloop/instagram
(Note: Requires PHP 7.2+ and Laravel/Symfony compatibility. Test in a staging environment first.)
Register the Service Provider
Add to config/app.php:
'providers' => [
// ...
Beloop\Instagram\InstagramServiceProvider::class,
],
Publish Configuration
php artisan vendor:publish --provider="Beloop\Instagram\InstagramServiceProvider" --tag=config
(No Laravel-specific config exists; adapt config/instagram.php manually.)
First Use Case: Fetch Public Posts
Inject the InstagramManager into a controller or service:
use Beloop\Instagram\InstagramManager;
public function __construct(private InstagramManager $instagram) {}
public function showFeed()
{
$posts = $this->instagram->getUserPosts('username');
return view('feed', compact('posts'));
}
(Assumes the package exposes a getUserPosts() method; verify via source.)
OAuth Setup (If Needed)
(Note: The package may lack OAuth support. Manually configure Instagram’s Graph API credentials in .env.)
INSTAGRAM_CLIENT_ID=your_id
INSTAGRAM_CLIENT_SECRET=your_secret
INSTAGRAM_REDIRECT_URI=http://your-app.com/callback
Data Fetching
InstagramManager facade or service to fetch public data:
$posts = Instagram::posts()->forUser('username')->limit(10)->get();
(Customize based on actual package methods.)public function getCachedPosts($username)
{
return Cache::remember("instagram_{$username}_posts", now()->addHours(1), function() use ($username) {
return $this->instagram->getUserPosts($username);
});
}
OAuth (If Supported)
return Instagram::auth()->redirect();
(Likely requires manual implementation due to archived status.)public function handleInstagramCallback()
{
$user = Instagram::auth()->user();
// Store user data in Laravel session/database.
}
Data Persistence
// Example model: app/Models/InstagramPost.php
class InstagramPost extends Model
{
protected $fillable = ['id', 'caption', 'media_url', 'user_id'];
}
public function run()
{
$posts = Instagram::posts()->forUser('username')->get();
InstagramPost::insert($posts->toArray());
}
API Rate Limiting
throttle middleware or a decorator:
public function getPostsWithThrottle($username)
{
return Cache::throttle('instagram_rate_limit', now()->addMinutes(1))->remember(
"instagram_{$username}_posts",
now()->addHours(1),
fn() => $this->instagram->getUserPosts($username)
);
}
Facade for Simplicity: Create a Laravel facade to abstract the package:
// app/Facades/Instagram.php
namespace App\Facades;
use Illuminate\Support\Facades\Facade;
class Instagram extends Facade
{
protected static function getFacadeAccessor() { return 'instagram'; }
}
(Bind the facade in AppServiceProvider.)
Event-Driven Updates: Dispatch Laravel events for Instagram data changes:
event(new InstagramPostsFetched($posts));
(Listen for updates in other services.)
Testing:
Mock the InstagramManager in tests:
$this->mock(InstagramManager::class)->shouldReceive('getUserPosts')
->once()->andReturn(collect([$fakePost]));
Deprecated API Endpoints
No Laravel Service Provider
AppServiceProvider:
$this->app->singleton('instagram', function ($app) {
return new Beloop\Instagram\InstagramManager($app['config']['instagram']);
});
OAuth2 Limitations
use Laravel\Socialite\Facades\Socialite;
$user = Socialite::driver('instagram')->user();
Symfony Dependencies
HttpClient). Replace with Laravel’s HttpClient:
use Illuminate\Support\Facades\Http;
$response = Http::withHeaders([
'Authorization' => 'Bearer ' . $token,
])->get('https://api.instagram.com/v1/users/self/media/recent');
No Caching Layer
Cache facade:
$posts = Cache::remember("instagram_{$username}_posts", now()->addHours(1), function() use ($username) {
return $this->instagram->getUserPosts($username);
});
Rate Limit Handling
use Illuminate\Support\Facades\Http;
$response = Http::timeout(30)->retry(3, 100)->get($url);
Data Model Mismatch
$posts = $this->instagram->getUserPosts($username)->map(function ($post) {
return [
'id' => $post['id'],
'caption' => $post['caption'] ?? '',
'media_url' => $post['images']['standard_resolution']['url'],
];
});
Enable API Debugging Add logging to track API calls:
\Log::debug('Instagram API Request', ['url' => $url, 'params' => $params]);
Mock Instagram API
Use Laravel’s Http mocking in tests:
Http::fake([
'api.instagram.com/*' => Http::response($fakeData, 200),
]);
Check for Deprecated Methods
Search the package source for @deprecated or trigger_error calls.
Validate OAuth Tokens If using OAuth, verify token expiration:
if (Carbon::parse($token['expires_at'])->isPast()) {
throw new \Exception('Token expired');
}
Custom API Endpoints
Extend the InstagramManager to support new endpoints:
class CustomInstagramManager extends Beloop\Instagram\InstagramManager
{
public function getStories($username)
{
return $this->http->get("/{$username}/stories");
}
}
Webhook Listeners Add Laravel event listeners for Instagram webhooks:
// app/Providers/EventServiceProvider.php
protected $listen = [
'instagram.webhook' => [
'App\Listeners\HandleInstagramWebhook',
],
];
Database Observers Use Eloquent observers to sync Instagram data:
class InstagramPostObserver
{
public function saved(InstagramPost $post)
{
// Sync with external service.
}
}
Artisan Commands Create commands for bulk operations:
php artisan instagram:fetch --username=test_user
(Define in app/Console/Commands/FetchInstagramPosts.php.)
.env includes:
INSTAGRAM_APP_ID
How can I help you explore Laravel packages today?