codebar-ag/laravel-instagram

This is my package laravel-instagram

v11.1 2025-01-30 03:24 UTC

This package is auto-updated.

Last update: 2025-01-30 03:25:13 UTC


README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

This package was developed to give you a quick start to communicate with the Instagram Api. It is used to query the most common endpoints.

This package is only designed to login with a single user account to display instagram data on your website. its not currently designed to be used as a multi-user application.

Navigation

🛠 Requirements

Installation

You can install the package via composer:

composer require codebar-ag/laravel-instagram

Then:

php artisan instagram:install

Or:

You can publish the config file with:

php artisan vendor:publish --tag="instagram-config"

This is the contents of the published config file:

<?php

return [

    /*
     * The client_id from registering your app on Instagram
     */
    'client_id' => env('INSTAGRAM_CLIENT_ID', null),

    /*
     * The client secret from registering your app on Instagram,
     * This is not the same as an access token.
     */
    'client_secret' => env('INSTAGRAM_CLIENT_SECRET', null),
];

You should finally add the following to your .env file:

INSTAGRAM_CLIENT_ID=your-client-id
INSTAGRAM_CLIENT_SECRET=your-client-secret

Overriding the default routes

If you want to override the default routes, you can do so by creating a instagram.php file in your routes directory and adding the following code:

<?php

use CodebarAg\LaravelInstagram\Http\Controllers\InstagramController;
use Illuminate\Support\Facades\Route;

Route::prefix('instagram')->name('instagram.')->group(function () {
    Route::get('/auth', [InstagramController::class, 'auth'])->name('auth');

    Route::get('/callback', [InstagramController::class, 'callback'])->name('callback');
});

Then you should register the routes in your bootstrap\app.php:

    ->withRouting(
        web: __DIR__ . '/../routes/web.php',
        //        api: __DIR__ . '/../routes/api.php',
        then: function () {
            Route::middleware('web')->group(base_path('routes/instagram.php'));
        },
    )

or in your RouteServiceProvider:

$this->routes(function () {
    Route::middleware('web')->group(base_path('routes/web.php'));
    Route::middleware('web')->group(base_path('routes/instagram.php'));
});

You can get your client id and client secret by registering your app on the Instagram Developer Portal

When configuring your app on the Instagram Developer Portal, you will need to set the redirect uri to: http://your-app-url.com/instagram/callback

You should also set the Deauthorize callback URL to: http://your-app-url.com/instagram/deauthorize

You should also set the Deletion callback URL to: http://your-app-url.com/instagram/delete

The links above need to be publicly accessible. You can use tools like Expose or ngrok to expose your local development environment to the internet. When using the tools above, ensure you set your APP_URL in your .env file to the url provided by the tool.

Usage

Authentication

To authenticate with the instagram api, you need to redirect the user to the following named route instagram.login or use the path /instagram/login.

This will redirect the user to the Instagram login page, where they will be asked to authorize your app to access their account.

After the user has authorized your app, they will be redirected back to your app, where you can then use the instagram facade to interact with the Instagram API.

Getting the connector

use CodebarAg\LaravelInstagram\Actions\InstagramHandler;

$connector = InstagramHandler::connector(); // returns an instance of \CodebarAg\LaravelInstagram\Connectors\InstagramConnector

Getting the user

use CodebarAg\LaravelInstagram\Requests\GetInstagramMe;

$response = $connector->send(new GetInstagramMe);

$user = $response->dto(); // returns an instance of \CodebarAg\LaravelInstagram\DTO\InstagramUser

Getting the user media

use CodebarAg\LaravelInstagram\Requests\GetInstagramMedia;

$response = $connector->send(new GetInstagramMedia);

$media = $response->dto(); // returns a collection of \CodebarAg\LaravelInstagram\DTO\InstagramImage

Getting the user media without nested children images

use CodebarAg\LaravelInstagram\Requests\GetInstagramMedia;

$response = $connector->send(new GetInstagramMedia(withChildren: false));

$media = $response->dto(); // returns a collection of \CodebarAg\LaravelInstagram\DTO\InstagramImage

DTO Showcase

InstagramUser

CodebarAg\LaravelInstagram\Data\InstagramUser {
    id: '987654321'                                             // string
    user_id: '123456789'                                        // string               
    username: 'john_doe'                                        // string
    name: 'John Doe'                                            // string
    account_type: 'BUSINESS'                                    // string
    profile_picture_url: https://instagram-link.com             // string
    followers_count: 200                                        // int
    follows_count: 100                                          // int
    media_count: 1                                              // int
}

InstagramImage

CodebarAg\LaravelInstagram\Data\InstagramImage {
    id: '123456789'                                             // string
    media_type: 'CAROUSEL_ALBUM'|'IMAGE'                        // string
    media_url: 'https://instagram-link.com'                     // string
    permalink: 'https://instagram-link.com'                     // string
    timestamp: '2022-01-01T00:00:00+00:00'                      // CarbonImmutable
    caption: 'This is a caption'                                // null|string
    children: [                                                 // null|Collection
        CodebarAg\LaravelInstagram\Data\InstagramImage {
            id: '123456798'                                     // string
            media_type: 'IMAGE'                                 // string
            media_url: 'https://instagram-link.com'             // string
            permalink: 'https://instagram-link.com'             // string
            timestamp: '2022-01-01T00:00:00+00:00'              // CarbonImmutable
            caption: null                                       // null
            children: null                                      // null
        }
    ]
}

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.