Blog / Development / API

How to Integrate Instagram API and Feed on PHP Website

  • Rating — 4.8 (246 voices)
  • by Tony
  • Updated on June 04, 2019
  • Read —
    4-5 minutes
Integration of Instagram API

Few can argue that Instagram is one of the most powerful and engaging social media platforms across the internet right now. It has more than 700 million monthly active users and about 400 million active users per day. No surprise that many merchants and companies are already using Instagram as their primary sale or promotion channel.

Taking into the account that 65% of customers are engaged in online shopping and social media, integrating Instagram may actually provide you a huge boost in sales and conversions. We’d like to share two most popular Instagram website integrations: social login and feed streaming and teach you how to integrate API into a website.

Login with Instagram Using PHP

It must be acknowledged that the shorter registration form – the higher conversion rates. Users are not willing to spend hours filling in long forms. The feature we’re going to integrate will allow your users to quickly login by using only their Instagram account password.

Moreover, making this function available you still will be able to collect all valuable user data required for targeting, email newsletters, and analytical purposes.

Information that can be obtained through Instagram API:

  • User info
  • User feeds
  • User media
  • User likes
  • User follows
  • User follower
  • User Relationship
  • Search media
  • Get media
  • Popular media
  • Media likes
  • Media comments
  • Like media
  • Delete like media
  • Get location
  • Get location media
  • Search location

Step #1 Registering your Application

Instagram dev mode

  • Login on www.instagram.com;
  • Go to the developer account https://www.instagram.com/developer/;
  • Click on “Register Your Application” button;
  • Register new client ID;
  • In “Website URL” field use your website address or localhost if you’re using it;
  • In “Redirect URL” field type location where the response will be handled (e.g. http://yourwebsite/yourprojectfolder/callback.php );
  • Go to Manage Clients tab. From here you can acquire your Client ID and Client Secret.

Step #2 User Flow

The principle of Instagram Authentication is quite simple. User clicks on “Login with Instagram” button. The request is redirected through Authorization URL to Instagram. It`s like magic.

Here’s the structure of Authorization URL:

https://api.instagram.com/oauth/authorize/?client_id=CLIENT-ID&redirect_uri=REDIRECT-URI&response_type=code

In response, Instagram will provide you with the code in the following format:

https://your-redirect-uri?code=CODE

Get this code from URL

Step #3 Access Token

Next, you’ll need an access token. In order to acquire it you’ll need to make an array with the following data:

  • Client ID.
  • Client Secret.
  • Redirect URL.
  • Grant Type.
  • Code from Step #2.
$url = "https://api.instagram.com/oauth/access_token";
$header = 0; // header = 0, because we do not have header
$data = array(
"client_id" => $client_id,
"client_secret" => $client_secret,
"redirect_uri" => $redirect_uri,
"grant_type" => "authorization_code",
"code" => $code
);

You can call it and pass this URL using CURL

Step #4 Getting User Information

Finally, after getting access_token you can call this URL https://api.instagram.com/v1/users/self/?access_token=$access_token) and get User Data.

Data you will acquire:

  • Full Name.
  • Website.
  • Bio.
  • Followers Count.
  • Following Count.
  • Profile Picture.

That’s it, and good luck.

Just kidding! Here is a folder with all files required for integration of Instagram Login on your website using PHP.

Enter your name and email to download the file.

How to integrate Instagram feed to your website without API

Most tutorials on how to implement Instagram feed suggest to use Instagram API. And while there is nothing wrong with this method, there is an easier way without actually using Instagram API.

GBKSOFT
Read Guide:
Basics of API development

The first thing we need to do is to get a feed data from Instagram.

Here’s a code sample for this task:

<?php
$username = 'YourUserName';
$json = file_get_contents('https://www.instagram.com/'.$username.'/media/');
$instagram_feed_data = json_decode($json, true);
?>

For the “name” field use your username as it shown in Instagram’s address line.

The string $instagram_feed_data = json_decode($json, true); indicate the data will be contained and decoded. The data will be decoded into the array.

Basically, in your array, the data about each post will be structured into [“items”]. Each item contains the information about every single post including user info, images, the caption, likes, comments, etc.

This is the information we’ll gonna use. But first, you should check if there are any items in the array. Here’s how to make this:

if (isset($instagram_feed_data['items'])) 
foreach ($instagram_feed_data['items'] as $item) }

Next, you’ll need to get an actual post link and image URL. Keep in mind that the array contains image URLs in multiple formats: thumbnail, low_resolution, and standard_resolution. You may choose one of those formats depending on your needs.

In our case, we’ll be ok with low resolution.

$link = $item['link'];
$img_url = $item['images']['low_resolution']['url'];

In addition, you can take a caption. In case of Instagram posts there isn’t always a caption, so you’ll need to check whether it exists first.

$caption = isset($item['caption']) ? $item['caption']['text'] : '';

Streaming

Now, when feed data is acquired we can stream to your webpage. Let’s start with the images by using image URLs. Since this URL is leading to an external web resource (Instagram), we’ll make target blank. Then, list an output of actual image and caption.

<a href="<?= $link; ?>" target="_blank" class="instagram-post">

<img src="<?= $img_url; ?>">
<div class="caption"><?= $caption; ?></div>

That’s it. All you need is to compose all this code strings into one script. Here’s it by the way.

if (isset($instagram_feed_data['items'])) {
    foreach ($instagram_feed_data['items'] as $item) {
        $link = $item['link'];
        $img_url = $item['images']['low_resolution']['url'];
        $caption = isset($item['caption']) ? $item['caption']['text'] : '';
        
        ?>
        <a href="<?= $link; ?>" target="_blank" class="instagram-post">
            <img src="<?= $img_url; ?>">
            <div class="caption"><?= $caption; ?></div>
        </a>
        <?php
    }
}
?>

Use this simple script that will get images, caption and links from the Instagram feed with no API. With little CSS magic, you can turn this data into such a good-looking web page.

integrate instagram feed to php website

Hope you found what you looked for. On any questions feel free to:


REFERENCES

  • https://www.instagram.com/developer/
  • https://developers.facebook.com/docs/instagram-api
GBKSOFT
Social Login PHP Code

Get Awesome PHP Libraries For Social Sing-in

Tony Sol is the business development manager of GBKSOFT, overseeing the production of all writings for both internal blog and external platforms. He is technical-driven person always looking for new benefits of merging business and software.


Comments (9)

Leave a comment
Close

Leave a Reply


Martin
Mar 25, 2019 at 9:35 PM

Is this tutorial in keeping with new Graph API?
Thanks you

reply
MAx
Mar 18, 2019 at 11:09 AM

Hi, thanks for this fantastic article!
I only have one question. You mentioned, that “making this function available you still will be able to collect all valuable user data required for targeting, email newsletters, and analytical purposes.” I don’t get this. If a user logs in via Instagram I can not ask him for mail-address and I don’t get the address from the instagram API. Right?

reply
Alex Pedchenko
Apr 11, 2019 at 11:15 AM
MAx,

Yes. But you can build own audience.

reply

WILMAR LIMA da SILVA
Feb 11, 2019 at 6:18 PM

Can I send messega auto to all my follower ?

reply
Alex Pedchenko
Feb 26, 2019 at 5:10 PM
WILMAR LIMA da SILVA,

Yes

reply

Victoria
Sep 17, 2018 at 2:55 PM

I really like your blogs. According to me, when a business plans to have a digital presence, it is very necessary to go for Website Development which can showcase their business digitally.

reply
Halder
Jul 23, 2018 at 12:13 AM

I Tried but I keep getting errors:
{“error_type”: “OAuthForbiddenException”, “code”: 403, “error_message”: “You are not a sandbox user of this client”}
Looks like this app needs manual approval from instagram as they have disabled permissions

reply
Siva
Mar 19, 2018 at 10:56 AM

now im building my web application
and i wanna use instrgram API
I followed this and i did as they say
last thing i arrived
i got the authantication aceess
and wrote the java script as they gave in the link
but when i load the page
nothing apperas

reply
Amir
Nov 12, 2017 at 9:39 AM

Great article.
Thanks

reply

Similar Blog Articles

StartUp App Ideas

7 Best Instagram-like Apps

Instagram is one of the most popular social networks in the world and definitely the most used photo sharing & editing mobile application. According to Statista, it has 1 billion monthly active users which practically means that every seventh person on Earth downloaded this app. The figure sounds even more impressive if we consider the...

Rating — 4.8 (89 voices)
АРІ Development

Development

Back to Basics: Before Starting the API Development

I bet you’ve already heard this three magic letters: API. What do they mean? Why you may possibly want to deal with API? How to approach this concept and make smart decisions dealing with API? Let’s start with the basics.

Rating — 5 (6 voices)
Is it relevant to build websites with PHP in 2018?

Development

Is it Still Relevant to Build Websites with PHP in 2019?

The world of the Internet is similar to the universe – there might be an end somewhere, but we can’t see it. Thousands of new websites are launched each and every day and this tendency doesn’t seem to stop. But have you ever wondered how are these sites are made? When it comes to choosing...

Rating — 4.8 (108 voices)

Categories

All articles Business Company News Development Marketing StartUp App Ideas UI and Design Uncategorized

People are talking about

You've got
a project in mind

What's next?

Sent us a message with a brief description of your project.
Our expert team will review it and get back to you within one business day with free consultation and to discuss the next steps.

Testimonials

Raphael Lasry
My Project with GBKSOFT gave me the ability to develop my software while keeping a busy schedule. Ana, who was my project manager, was very professional and was always understanding of my vision and what I wanted. I would recommend GBKSOFT again to any other company or person who has a vision for their web application. Thank you GBKSOFT! Recommend
Aaron
I’ve been using GBK Soft for the past 3 years and they have been great. Communication is unparalleled to other app development companies. I’ve continued to return to them to improve my iOS app countless times and I will continue to do so in the future. I highly recommend this company! Improve
Andy
I think they do great work. I haven’t yet given them something that they were unable to do. Great
13
App Futura ExtractGood FirmsClutchAwwwards