Search code examples
phpinstagramembedlaravel-7

Add/Embed Instagram Posts To Laravel 7


What I want: I am creating a blog using Laravel 7 for my client. He wants to add a section containing a link to his Instagram posts by something similar to a carousel, so that, when he posts a new Instagram post the website will be automatically updated.

What I did:

  1. I went to developers.facebook.com, created a new app and setup Instagram Basic Display This Picture where I got Instagram App Id, Secret and Name.
  2. Added Client OAuth Settings to be for localhost, it forced me to add ssl (https) I know this won't work, I will change it to the actual domain later, and I hope for a way to test that on localhost too. OAuth.
  3. I added the Instagram tester and authorized it and generated an access token.Tester Token
  4. I installed composer require dymantic/laravel-instagram-feed from github-repo
  5. I ran the command php artisan vendor:publish which showed me these options
Which provider or tag's files would you like to publish?:
  [0 ] Publish files from all providers and tags listed below
  [1 ] Provider: Dymantic\InstagramFeed\InstagramFeedServiceProvider
  [2 ] Provider: Facade\Ignition\IgnitionServiceProvider

So I entered 1 the I ran php artisan migrate; which created the following file I updated the client id and secret to the data shown in the first photo I linked:

// config/instagram-feed.php

<?php

return [
    /*
     * The client_id from registering your app on Instagram
     */
    'client_id'           => 'YOUR INSTAGRAM CLIENT ID',

    /*
     * The client secret from registering your app on Instagram,
     * This is not the same as an access token.
     */
    'client_secret'       => 'YOUR INSTAGRAM CLIENT SECRET',

    /*
     * The route that will respond to the Instagram callback during the OAuth process.
     * Only enter the path without the leading slash. You need to ensure that you have registered
     * a redirect_uri for your instagram app that is equal to combining the
     *  app url (from config) and this route
     */
    'auth_callback_route' => 'instagram/auth/callback',

    /*
     * On success of the OAuth process you will be redirected to this route.
     * You may use query strings to carry messages
     */
    'success_redirect_to' => 'instagram-auth-success',

    /*
     * If the OAuth process fails for some reason you will be redirected to this route.
     * You may use query strings to carry messages
     */
    'failure_redirect_to' => 'instagram-auth-failure'

    /*
     * You may filter out video media types by setting this to true. Carousel media
     * will become the first image in the carousel, and if there are no images, then
     * the entire carousel will be ignored.
     */
     'ignore_video' => false,

    /*
     * You may set an email address below if you wish to be notified of errors when
     * attempting to refresh the Instagram feed.
     */
    'notify_on_error' => null,
];
  1. I added the following trait after running the command php artisan instagram-feed:profile my-insta-username:
<?php

namespace App\Traits;

use \Dymantic\InstagramFeed\Profile;

trait Instagram
{
    public static function feed()
    {
        $profile = Profile::where('username', 'my-insta-username')->first();

        $feed = $profile->feed();
        
        dd($profile);
    }
}

Result and Conclusion: Obviously this didn't work, function $profile->feed(); is unknown since the profile would be only a collection we don't have access to the Instagram API yet.

Question: Any idea to get the needed data how can I benefit from what I already did, even if I can get the same result using a curl request it would be fine??

THANK YOU :)


Solution

  • If what you want to achieve is to embed your Ig feed images into a carousel I'd recommend you to use postaddic/instragram package (https://github.com/postaddictme/instagram-php-scraper).

    With this package you can easily fetch the URL of the image like so:

    $instagram  = Instagram::withCredentials('login', 'password', new Psr16Adapter('Files'));
    $instagram->login();
    $instagram->saveSession();
    
    $posts  = $instagram->getFeed();
    
    foreach ($posts as $post){
        echo $post->getImageHighResolutionUrl()."\n";
    }
    

    If you want to test, I found this was the simplest method for fetching public accounts:

    $instagram = new \InstagramScraper\Instagram();
    
    // For getting information about account you don't need to auth:
    
    $account = $instagram->getAccount('neymarjr'); 
    

    If the profile is public even better since you can use methods that dont require your credentials for authentication.

    The hassle from embbeding photos directly from Ig API is not required.