In this tutorial, we are going to see how to show Instagram feed on website using laravel. We are going to create real time image gallery using Instagram post api. This will help you fetch your most recent pictures and account information of the Instagram directly on the website.
If you own a profile or a travel agency website, it will help you get your Instagram feeds inside the website so, that you dont have to upload photos in two different place and also get the real time data via Instagram API.
First thing first, we need the Instagram API credentials so, head over to this article where i wrote how to generate Instagram API acess token and take a note of it. We will be needing it to fetch the real time data from your account. Next, we are going to use Instagram Package by Vinkala (Vincent Kaliber) which will help us to get the data from the Instagram using our API and access token.
We are also going to install some third party packages called php/http-message
and php/guzzle6-adapter
, which is required for Instagram package to make http calls to the Instagram API. We will look step by step process in order to achieve the result.
Let’s get started,
What are we going to do ?
- Fetching the necessary packages from GitHub repository.
- Adding access token inside laravel config file.
- Create new controller and configure routes.
- Finally fetch the data in your blade template.
Step 1 : Fetching the Necessary Packages from GitHub Repository
In this step we are going to pull some packages into our laravel application, go to terminal and then import these packages.
- Instagram package –
$ composer require vinkla/instagram
- php/http-message –
$ composer require php-http/message
- php/guzzle6 adapter –
$ composer require php-http/guzzle6-adapter
Or you can install all at once $ composer require vinkla/instagram php-http/message php-http/guzzle6-adapter
Once installed make sure that you php artisan dump-autoload
, then after php artisan config:clear
and cache:clear
to remove all your caches are reload the config, because of the users are facing “ClassNotFoundException” issue while using this package.
Step 2 : Adding Access Token Inside Config File
The access token that you have generated from the third paragraph in this post, now, it is time to use it. We can use it inside the controller but lets make things cleaner and add this access token inside application root/app/config/services.php
so that we could reuse it anywhere inside the application.
1 2 3 |
'instagram_api' => [ 'token' => 'xxxxxxxxx.xxxxxx.xxxxxxxxxxxxxxx', // paste your access token inside quotes ], |
We just have to type config(services.instagram_api.token)
to access the token that we had just kept inside the services.php
Step 3 : Creating Controller and Configuring Routes.
In this step we are going to create new controller and configure routes to hit the api call.
Since, i am going to use Instagram API call, i’ll name it ApiController.php
and use this controller for all the third party API calls.
Go ahead and make controller called ApiController.php
and add this block of code inside it.
1 2 3 4 5 6 7 8 9 10 |
public function getInstagramFeed() { $instagram = new Instagram(config('services.instagram_api.token')); // new Instagram('xxxxx.xxx.xxxxxxxxxxxxxxx') access token can be directly used here. $ig_images = []; foreach($instagram->get() as $insta_details) { $ig_images[] = $insta_details->images->standard_resolution->url; } return view('index', ['ig_images'=>$ig_images]); } |
To make this implementation even more simple, we can make use of laravel collections,
1 2 3 |
$ig_images = collect($instagram->get())->map(function ($item) { return $item->images->standard_resolution->url; }); |
Optional :
To be more clear what inside the Instagram class object we can run dump server which is just added new to laravel 5.7, which will help us debug more clearly. Refer to our the new coming of laravel 5.7 to know more about it.
Open new terminal window and type
php artisan dump
.Go back to code and inside
getInstagram();
method comment all the codes below$instagram = new Instagram(config('services.instagram_api.token'));
anddump($instagram->get());
and reload the application to see what we have fetched using the Instagram API. You will see all the details inside the dump server windows like below.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462 Laravel Var Dump Server=======================[OK] Server listening on tcp://127.0.0.1:9912// Quit the server with CONTROL-C.GET http://localhost:8000/instagram-feed---------------------------------------------------- ----------------------------------------date Wed, 12 Sep 2018 02:34:20 +0000controller "ApiController"source ApiController.php on line 12file app\Http\Controllers\ApiController.php------------ ----------------------------------------array:8 [0 => {#235+"id": "1861782140564403504_8016644362"+"user": {#234+"id": "8016644362"+"full_name": "Travel To Nepal 😍🇳🇵"+"profile_picture": "https://scontent.cdninstagram.com/vp/36790f8e90fac1955fa112e312b00684/5C323C04/t51.2885-19/s150x150/39248393_290930611489538_4252559360149946368_n.jpg"+"username": "itravel2nepal"}+"images": {#236+"thumbnail": {#246+"width": 150+"height": 150+"url": "https://scontent.cdninstagram.com/vp/8e16916ecff17af735730d00d40b1db2/5C34A775/t51.2885-15/e35/c0.71.567.567/s150x150/40065590_532432193865209_1177565898102276096_n.jpg"}+"low_resolution": {#239+"width": 320+"height": 400+"url": "https://scontent.cdninstagram.com/vp/d0017bb9dee058f938d839495bb051a6/5C1E874E/t51.2885-15/e35/p320x320/40065590_532432193865209_1177565898102276096_n.jpg"}+"standard_resolution": {#238+"width": 640+"height": 800+"url": "https://scontent.cdninstagram.com/vp/609a4324247b06a3c05f46b4eba8814e/5C16F2EF/t51.2885-15/e35/40065590_532432193865209_1177565898102276096_n.jpg"}}+"created_time": "1536161747"+"caption": {#245+"id": "17917027330200584"+"text": "#nature #mountain #beautiful #cricket"+"created_time": "1536161747"+"from": {#232+"id": "8016644362"+"full_name": "Travel To Nepal 😍🇳🇵"+"profile_picture": "https://scontent.cdninstagram.com/vp/36790f8e90fac1955fa112e312b00684/5C323C04/t51.2885-19/s150x150/39248393_290930611489538_4252559360149946368_n.jpg"+"username": "itravel2nepal"}}+"user_has_liked": false+"likes": {#231+"count": 5}+"tags": array:4 [0 => "nature"1 => "beautiful"2 => "mountain"3 => "cricket"]+"filter": "Clarendon"+"comments": {#244+"count": 0}+"type": "image"+"link": "https://www.instagram.com/p/BnWYP4hgfUw/"+"location": {#247+"latitude": 35.239166666667+"longitude": 74.59+"name": "Nanga Parbat"+"id": 354689810}+"attribution": null+"users_in_photo": []}1 => {#248+"id": "1858040490910540523_8016644362"+"user": {#249+"id": "8016644362"+"full_name": "Travel To Nepal 😍🇳🇵"+"profile_picture": "https://scontent.cdninstagram.com/vp/36790f8e90fac1955fa112e312b00684/5C323C04/t51.2885-19/s150x150/39248393_290930611489538_4252559360149946368_n.jpg"+"username": "itravel2nepal"}+"images": {#251+"thumbnail": {#250+"width": 150+"height": 150+"url": "https://scontent.cdninstagram.com/vp/64a5e7ac7126872b8e5dbe57ac6afcc2/5C372F3B/t51.2885-15/e35/c0.135.1080.1080/s150x150/39386880_240295889922408_1479121869851328512_n.jpg"}+"low_resolution": {#252+"width": 320+"height": 400+"url": "https://scontent.cdninstagram.com/vp/6226681ec2d92013b4668b43525071f6/5C1798C2/t51.2885-15/e35/p320x320/39386880_240295889922408_1479121869851328512_n.jpg"}+"standard_resolution": {#253+"width": 640+"height": 800+"url": "https://scontent.cdninstagram.com/vp/bf4005aeef7b2186fe50560a19c07b52/5C1F8095/t51.2885-15/sh0.08/e35/p640x640/39386880_240295889922408_1479121869851328512_n.jpg"}}+"created_time": "1535715708"+"caption": null+"user_has_liked": false+"likes": {#254+"count": 7}+"tags": []+"filter": "Clarendon"+"comments": {#255+"count": 0}+"type": "image"+"link": "https://www.instagram.com/p/BnJFftvA-Lr/"+"location": null+"attribution": null+"users_in_photo": []}2 => {#256+"id": "1857911338224005867_8016644362"+"user": {#257+"id": "8016644362"+"full_name": "Travel To Nepal 😍🇳🇵"+"profile_picture": "https://scontent.cdninstagram.com/vp/36790f8e90fac1955fa112e312b00684/5C323C04/t51.2885-19/s150x150/39248393_290930611489538_4252559360149946368_n.jpg"+"username": "itravel2nepal"}+"images": {#259+"thumbnail": {#258+"width": 150+"height": 150+"url": "https://scontent.cdninstagram.com/vp/f8eb1e53a07246b4d93f9079fae6a4d9/5C31BCD2/t51.2885-15/e35/c0.80.640.640/s150x150/40384337_1905193976205964_2314924429798277120_n.jpg"}+"low_resolution": {#260+"width": 320+"height": 400+"url": "https://scontent.cdninstagram.com/vp/9f168f5f7d034575234ed2cd33a39436/5C2CBB97/t51.2885-15/e35/p320x320/40384337_1905193976205964_2314924429798277120_n.jpg"}+"standard_resolution": {#261+"width": 640+"height": 800+"url": "https://scontent.cdninstagram.com/vp/f4f8b791053b525afb0207ec246da4e2/5C264300/t51.2885-15/e35/40384337_1905193976205964_2314924429798277120_n.jpg"}}+"created_time": "1535700312"+"caption": null+"user_has_liked": false+"likes": {#262+"count": 6}+"tags": []+"filter": "Clarendon"+"comments": {#263+"count": 0}+"type": "image"+"link": "https://www.instagram.com/p/BnIoIS7AzLr/"+"location": null+"attribution": null+"users_in_photo": []}3 => {#264+"id": "1857037779591222645_8016644362"+"user": {#265+"id": "8016644362"+"full_name": "Travel To Nepal 😍🇳🇵"+"profile_picture": "https://scontent.cdninstagram.com/vp/36790f8e90fac1955fa112e312b00684/5C323C04/t51.2885-19/s150x150/39248393_290930611489538_4252559360149946368_n.jpg"+"username": "itravel2nepal"}+"images": {#267+"thumbnail": {#266+"width": 150+"height": 150+"url": "https://scontent.cdninstagram.com/vp/59555fa4545bb6080e3bcfa0bf13feca/5C370C9D/t51.2885-15/e35/c0.92.1080.1080/s150x150/40083450_908788472653919_713225842586550272_n.jpg"}+"low_resolution": {#268+"width": 320+"height": 374+"url": "https://scontent.cdninstagram.com/vp/5ba63aa0392c5d7cd8a83c6d706ea3bf/5C2B8FD3/t51.2885-15/e35/p320x320/40083450_908788472653919_713225842586550272_n.jpg"}+"standard_resolution": {#269+"width": 640+"height": 749+"url": "https://scontent.cdninstagram.com/vp/4466db58820139f64558d69d39da78f1/5C23843F/t51.2885-15/sh0.08/e35/p640x640/40083450_908788472653919_713225842586550272_n.jpg"}}+"created_time": "1535596176"+"caption": {#270+"id": "17969181208067195"+"text": "#travelling #travel #tour #trek #nepal #bajura #heaven"+"created_time": "1535596176"+"from": {#271+"id": "8016644362"+"full_name": "Travel To Nepal 😍🇳🇵"+"profile_picture": "https://scontent.cdninstagram.com/vp/36790f8e90fac1955fa112e312b00684/5C323C04/t51.2885-19/s150x150/39248393_290930611489538_4252559360149946368_n.jpg"+"username": "itravel2nepal"}}+"user_has_liked": false+"likes": {#272+"count": 13}+"tags": array:7 [0 => "bajura"1 => "travelling"2 => "nepal"3 => "tour"4 => "heaven"5 => "trek"6 => "travel"]+"filter": "Clarendon"+"comments": {#273+"count": 0}+"type": "image"+"link": "https://www.instagram.com/p/BnFhgWDA9V1/"+"location": {#274+"latitude": 29.3833+"longitude": 81.3167+"name": "Bajura, Nepal"+"id": 297414239}+"attribution": null+"users_in_photo": []}4 => {#275+"id": "1857029186930193104_8016644362"+"user": {#276+"id": "8016644362"+"full_name": "Travel To Nepal 😍🇳🇵"+"profile_picture": "https://scontent.cdninstagram.com/vp/36790f8e90fac1955fa112e312b00684/5C323C04/t51.2885-19/s150x150/39248393_290930611489538_4252559360149946368_n.jpg"+"username": "itravel2nepal"}+"images": {#278+"thumbnail": {#277+"width": 150+"height": 150+"url": "https://scontent.cdninstagram.com/vp/1d6347f81876f9c6ead1732a77f490d0/5C2374F8/t51.2885-15/e35/c160.0.640.640/s150x150/39803335_1718979661562723_6146814738559926272_n.jpg"}+"low_resolution": {#279+"width": 320+"height": 213+"url": "https://scontent.cdninstagram.com/vp/2f3f820afda8075a471c9a5308e83bc2/5C17ADD4/t51.2885-15/e35/s320x320/39803335_1718979661562723_6146814738559926272_n.jpg"}+"standard_resolution": {#280+"width": 640+"height": 426+"url": "https://scontent.cdninstagram.com/vp/aab5375b80cae568e404951fd4207560/5C20D829/t51.2885-15/sh0.08/e35/s640x640/39803335_1718979661562723_6146814738559926272_n.jpg"}}+"created_time": "1535595151"+"caption": {#281+"id": "17975321602005406"+"text": "#travelling #travel #tour #trek #nepal #bajura #heaven"+"created_time": "1535595151"+"from": {#282+"id": "8016644362"+"full_name": "Travel To Nepal 😍🇳🇵"+"profile_picture": "https://scontent.cdninstagram.com/vp/36790f8e90fac1955fa112e312b00684/5C323C04/t51.2885-19/s150x150/39248393_290930611489538_4252559360149946368_n.jpg"+"username": "itravel2nepal"}}+"user_has_liked": false+"likes": {#283+"count": 11}+"tags": array:7 [0 => "bajura"1 => "travelling"2 => "nepal"3 => "tour"4 => "heaven"5 => "trek"6 => "travel"]+"filter": "Clarendon"+"comments": {#284+"count": 0}+"type": "image"+"link": "https://www.instagram.com/p/BnFfjTggbLQ/"+"location": {#285+"latitude": 29.3833+"longitude": 81.3167+"name": "Bajura, Nepal"+"id": 297414239}+"attribution": null+"users_in_photo": []}5 => {#286+"id": "1856340334733389825_8016644362"+"user": {#287+"id": "8016644362"+"full_name": "Travel To Nepal 😍🇳🇵"+"profile_picture": "https://scontent.cdninstagram.com/vp/36790f8e90fac1955fa112e312b00684/5C323C04/t51.2885-19/s150x150/39248393_290930611489538_4252559360149946368_n.jpg"+"username": "itravel2nepal"}+"images": {#289+"thumbnail": {#288+"width": 150+"height": 150+"url": "https://scontent.cdninstagram.com/vp/87d4f0b6c72f80f746358078c974e91e/5C32C6A2/t51.2885-15/e35/s150x150/39506611_504844986594043_2361962791364984832_n.jpg"}+"low_resolution": {#290+"width": 320+"height": 320+"url": "https://scontent.cdninstagram.com/vp/d086175abe2cb284e6dd6da8d34fdb20/5C305F52/t51.2885-15/e35/s320x320/39506611_504844986594043_2361962791364984832_n.jpg"}+"standard_resolution": {#291+"width": 640+"height": 640+"url": "https://scontent.cdninstagram.com/vp/b3f615dbc16d70d1b1a7a06fc1c3d4c9/5C37AE05/t51.2885-15/sh0.08/e35/s640x640/39506611_504844986594043_2361962791364984832_n.jpg"}}+"created_time": "1535513034"+"caption": null+"user_has_liked": false+"likes": {#292+"count": 14}+"tags": []+"filter": "Clarendon"+"comments": {#293+"count": 0}+"type": "image"+"link": "https://www.instagram.com/p/BnDC7L6A3AB/"+"location": {#294+"latitude": 27.861111111111+"longitude": 86.861388888889+"name": "Ama Dablam"+"id": 1.0816193254576E+14}+"attribution": null+"users_in_photo": []}6 => {#295+"id": "1855938598784754246_8016644362"+"user": {#296+"id": "8016644362"+"full_name": "Travel To Nepal 😍🇳🇵"+"profile_picture": "https://scontent.cdninstagram.com/vp/36790f8e90fac1955fa112e312b00684/5C323C04/t51.2885-19/s150x150/39248393_290930611489538_4252559360149946368_n.jpg"+"username": "itravel2nepal"}+"images": {#298+"thumbnail": {#297+"width": 150+"height": 150+"url": "https://scontent.cdninstagram.com/vp/8efbd83dd2119775c13d07908a0b99ce/5C1A35DB/t51.2885-15/e35/c14.0.691.691/s150x150/39921977_2030983106933466_3295970933881176064_n.jpg"}+"low_resolution": {#299+"width": 320+"height": 307+"url": "https://scontent.cdninstagram.com/vp/597c2338e015896df75a409e5172a2ec/5C364725/t51.2885-15/e35/s320x320/39921977_2030983106933466_3295970933881176064_n.jpg"}+"standard_resolution": {#300+"width": 640+"height": 615+"url": "https://scontent.cdninstagram.com/vp/e5ae00141bcf9cee3c16a382c540e4c4/5C3569D8/t51.2885-15/sh0.08/e35/s640x640/39921977_2030983106933466_3295970933881176064_n.jpg"}}+"created_time": "1535465143"+"caption": {#301+"id": "17900550895237649"+"text": "#Nepal #jumla"+"created_time": "1535465143"+"from": {#302+"id": "8016644362"+"full_name": "Travel To Nepal 😍🇳🇵"+"profile_picture": "https://scontent.cdninstagram.com/vp/36790f8e90fac1955fa112e312b00684/5C323C04/t51.2885-19/s150x150/39248393_290930611489538_4252559360149946368_n.jpg"+"username": "itravel2nepal"}}+"user_has_liked": false+"likes": {#303+"count": 9}+"tags": array:2 [0 => "nepal"1 => "jumla"]+"filter": "Clarendon"+"comments": {#304+"count": 1}+"type": "image"+"link": "https://www.instagram.com/p/BnBnlKJgGJG/"+"location": {#305+"latitude": 23.42657+"longitude": 85.73424+"name": "Jumla"+"id": 6.6649785020756E+14}+"attribution": null+"users_in_photo": []}7 => {#306+"id": "1855937452112487383_8016644362"+"user": {#307+"id": "8016644362"+"full_name": "Travel To Nepal 😍🇳🇵"+"profile_picture": "https://scontent.cdninstagram.com/vp/36790f8e90fac1955fa112e312b00684/5C323C04/t51.2885-19/s150x150/39248393_290930611489538_4252559360149946368_n.jpg"+"username": "itravel2nepal"}+"images": {#309+"thumbnail": {#308+"width": 150+"height": 150+"url": "https://scontent.cdninstagram.com/vp/c36c242959e6b046796fb22edce3b4b7/5C2155AB/t51.2885-15/e35/c72.0.425.425/s150x150/39364469_321323645287227_7343053771485741056_n.jpg"}+"low_resolution": {#310+"width": 320+"height": 238+"url": "https://scontent.cdninstagram.com/vp/a36f3787c78b19872aaff5b4c4f1c7e9/5C26FD72/t51.2885-15/e35/s320x320/39364469_321323645287227_7343053771485741056_n.jpg"}+"standard_resolution": {#311+"width": 640+"height": 477+"url": "https://scontent.cdninstagram.com/vp/c9aa8c983148a32e5ed3de792708f098/5C3113C0/t51.2885-15/e35/39364469_321323645287227_7343053771485741056_n.jpg"}}+"created_time": "1535465006"+"caption": {#312+"id": "17953250023082578"+"text": "#travel #diaries #trek #nepal"+"created_time": "1535465006"+"from": {#313+"id": "8016644362"+"full_name": "Travel To Nepal 😍🇳🇵"+"profile_picture": "https://scontent.cdninstagram.com/vp/36790f8e90fac1955fa112e312b00684/5C323C04/t51.2885-19/s150x150/39248393_290930611489538_4252559360149946368_n.jpg"+"username": "itravel2nepal"}}+"user_has_liked": false+"likes": {#314+"count": 10}+"tags": array:4 [0 => "nepal"1 => "trek"2 => "travel"3 => "diaries"]+"filter": "Clarendon"+"comments": {#315+"count": 2}+"type": "image"+"link": "https://www.instagram.com/p/BnBnUeOgiPX/"+"location": null+"attribution": null+"users_in_photo": []}]
Now, configure the routes by going to routes/web.php
.
1 |
Route::get("instagram-feed","ApiController@getInstagramFeed"); |
Step 4 : Fetching the Images Into the Blade Template
In this step we are going to display the images that have been pulled inside the ApiController.php
into the blade template. Since my ApiController
is pointing to my index page. I will just use normal foreach loop to loop over the image. Also, I have used bootstrap to make the dummy design quickly. Finally your code should look like this.
1 2 3 4 5 6 7 8 9 10 |
<div class="container"> <h1 class="h1">Instagram Gallery</h1> <div class="row"> @foreach($ig_images as $image) <div class="col-md-4" style="margin-bottom: 10px;"> <img src="{{$image}}" alt="" style="width:100%; height:300px;" /> </div> @endforeach </div> </div> |
After everything is complete, you should see your Instagram images in the index page. If you want, you can install jQuery Light box to make image zoom in a click and display as thumbnail and treat like an slide show.
This is it for this part of tutorial. If you went through any problem, make sure to mention them in comments.
Leave a Reply