Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Instagram Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Steps

  1. Install the Package

    composer require beloop/instagram
    

    (Note: Requires PHP 7.2+ and Laravel/Symfony compatibility. Test in a staging environment first.)

  2. Register the Service Provider Add to config/app.php:

    'providers' => [
        // ...
        Beloop\Instagram\InstagramServiceProvider::class,
    ],
    
  3. Publish Configuration

    php artisan vendor:publish --provider="Beloop\Instagram\InstagramServiceProvider" --tag=config
    

    (No Laravel-specific config exists; adapt config/instagram.php manually.)

  4. 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.)

  5. 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
    

Implementation Patterns

Core Workflows

  1. Data Fetching

    • Pattern: Use the InstagramManager facade or service to fetch public data:
      $posts = Instagram::posts()->forUser('username')->limit(10)->get();
      
      (Customize based on actual package methods.)
    • Laravel Integration: Wrap calls in a Laravel service for caching:
      public function getCachedPosts($username)
      {
          return Cache::remember("instagram_{$username}_posts", now()->addHours(1), function() use ($username) {
              return $this->instagram->getUserPosts($username);
          });
      }
      
  2. OAuth (If Supported)

    • Pattern: Redirect users to Instagram for auth (if the package supports it):
      return Instagram::auth()->redirect();
      
      (Likely requires manual implementation due to archived status.)
    • Callback Handling:
      public function handleInstagramCallback()
      {
          $user = Instagram::auth()->user();
          // Store user data in Laravel session/database.
      }
      
  3. Data Persistence

    • Pattern: Use Eloquent models to store fetched data:
      // Example model: app/Models/InstagramPost.php
      class InstagramPost extends Model
      {
          protected $fillable = ['id', 'caption', 'media_url', 'user_id'];
      }
      
    • Seeding:
      public function run()
      {
          $posts = Instagram::posts()->forUser('username')->get();
          InstagramPost::insert($posts->toArray());
      }
      
  4. API Rate Limiting

    • Pattern: Implement Laravel’s 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)
          );
      }
      

Integration Tips

  • 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]));
    

Gotchas and Tips

Pitfalls

  1. Deprecated API Endpoints

    • The package uses Instagram’s 2019-era API, which may no longer work. Verify endpoints via Meta’s API docs.
    • Fix: Override methods in a custom service to use current endpoints.
  2. No Laravel Service Provider

    • The package lacks Laravel-specific providers. Manually bind services in AppServiceProvider:
      $this->app->singleton('instagram', function ($app) {
          return new Beloop\Instagram\InstagramManager($app['config']['instagram']);
      });
      
  3. OAuth2 Limitations

    • Likely no built-in OAuth2 support for private data. Implement manually:
      use Laravel\Socialite\Facades\Socialite;
      
      $user = Socialite::driver('instagram')->user();
      
    • (Note: Instagram’s OAuth2 is deprecated; use Graph API instead.)
  4. Symfony Dependencies

    • The package may use Symfony components (e.g., 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');
      
  5. No Caching Layer

    • The package lacks caching. Add Laravel’s Cache facade:
      $posts = Cache::remember("instagram_{$username}_posts", now()->addHours(1), function() use ($username) {
          return $this->instagram->getUserPosts($username);
      });
      
  6. Rate Limit Handling

    • Instagram’s API enforces rate limits (e.g., 50 calls/hour). Implement retries:
      use Illuminate\Support\Facades\Http;
      
      $response = Http::timeout(30)->retry(3, 100)->get($url);
      
  7. Data Model Mismatch

    • The package’s response structure may not match Laravel’s Eloquent. Normalize data:
      $posts = $this->instagram->getUserPosts($username)->map(function ($post) {
          return [
              'id' => $post['id'],
              'caption' => $post['caption'] ?? '',
              'media_url' => $post['images']['standard_resolution']['url'],
          ];
      });
      

Debugging Tips

  1. Enable API Debugging Add logging to track API calls:

    \Log::debug('Instagram API Request', ['url' => $url, 'params' => $params]);
    
  2. Mock Instagram API Use Laravel’s Http mocking in tests:

    Http::fake([
        'api.instagram.com/*' => Http::response($fakeData, 200),
    ]);
    
  3. Check for Deprecated Methods Search the package source for @deprecated or trigger_error calls.

  4. Validate OAuth Tokens If using OAuth, verify token expiration:

    if (Carbon::parse($token['expires_at'])->isPast()) {
        throw new \Exception('Token expired');
    }
    

Extension Points

  1. 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");
        }
    }
    
  2. Webhook Listeners Add Laravel event listeners for Instagram webhooks:

    // app/Providers/EventServiceProvider.php
    protected $listen = [
        'instagram.webhook' => [
            'App\Listeners\HandleInstagramWebhook',
        ],
    ];
    
  3. Database Observers Use Eloquent observers to sync Instagram data:

    class InstagramPostObserver
    {
        public function saved(InstagramPost $post)
        {
            // Sync with external service.
        }
    }
    
  4. Artisan Commands Create commands for bulk operations:

    php artisan instagram:fetch --username=test_user
    

    (Define in app/Console/Commands/FetchInstagramPosts.php.)

Config Quirks

  1. Environment Variables Ensure .env includes:
    INSTAGRAM_APP_ID
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
babenkoivan/elastic-client
innmind/static-analysis
innmind/coding-standard
datacore/hub-sdk
alengo/sulu-http-cache-bundle
develia/commons
cuci/prototurk-sdk
cuci/prototurk-sdk-symfony
develia/geo-bundle
dreamzy/livewire-charts
touchestate-sdk/php-sdk
22h/doctrine-garbage-collection-bundle
agtp/agtp-php
agtp/mod-php
splash/sonata-admin
splash/metadata
splash/openapi
splash/scopes
splash/toolkit
testo/output-teamcity