Instagram API – Fetch Photos With a Specific Hashtag using PHP

instagram

Lately, I have been working with Instagram API to build a social networking website where I used many of its features including showing the public images under a specific #hashtag.

Hashtag is brilliant social media feature, which is already being used in all major social networking websites including Facebook and Twitter. In this post, you will see how we can grab all images from Instagram with a specific hashtag using its API and PHP.

Step 1:

Each API needs registration before you can use its features. Please note, for showing images under a specific hashtag, we don’t need any authentication but for connecting to Instagram API we do need client ID and client secret keys which we will get once we will register an app on Instagram API platform.

So our first step will be to register an app on Instagram API page https://instagram.com/developer/ and click on “Register Your Application” button.

st1

Step 2:

Next page is Manage Clients page, where we need to add a client to get the required authentication keys, i-e Client ID and Client Secret. Please click on “Register New Client” button to see the client registration form.

1

Step 3:

In this form , we will provide app name which should represent your website or company name for easily understandable for the end users. I used “99Points Hashtag” here as an example. Provide a short but clear description to let user know what is this app for. Add website and redirect URL. Both URLs can be same as we don’t need it here for showing the images only. Enter the information and click on “Register” button.

2

Step 4:

The next screen shows the success message with Client ID and Client Secret keys which we will use in our last step.

3

Step 5:

Finally, we have created the app on the Instagram, now its PHP-Action-Time. The below API url is a direct way to bring all the images having a specific hashtag. It takes hashtag and client ID which we already have above and then it brings the results in JSON format. Is it not easy?

Just put your tag and client ID in the URL and you will see all images but wait we need to read all images and for this we will parse this url from a Curl function and then we will decode the JSON result.

That’s where we need a curl function to execute the api URL. You can use this function any place where you need to execute or access an external URL within from our PHP website.

Next thing is to build our API url. We have now added tag and client ID and will execute the curl function.

Just pass the above line to our curl function which will return us JSON encoded output and we can easily decode that using PHP “json_decode” function as shown below. You can also print the result array to see what type of data it contains.

To go through all results, we will use simple loop:

For better understanding, we can print the $decoded_results array which will have all the data it brought.

res

Full Code:

In our upcoming tutorials, we will talk more about Instagram API and will show you how to build your own small Instagram application using its API.

13 thoughts on “Instagram API – Fetch Photos With a Specific Hashtag using PHP”

  1. Hi, thanks for the share.

    Do you have a video or pictures of the end result?

    How would this integrate with wordpress using the genesis theme and child theme?

    Also, ive noticed that instagram feeds affects the speed of the internet. I use google page speed insights. Will performance be affected?

    Thanks heaps :)
    Paul

  2. Hello,

    Thanks for the share this code

    I have tested in my wordpress site and it’s work but when i code put in server so it’s not work

    echo ‘

    ';
    
     print_r($decoded_results);
    
     exit;
    
    when i run above code give a null value any idea what's error. Also i have checked server config
    
    cURL is Enable
    
    Thanks
  3. Fini, ne marche plus, il faut que Instagram valide l’utilisation par une soumission !!! N’importe quoi…

  4. Hi there,

    I am using your tutorial and now i get this error

    Array
    (
    [pagination] => Array
    (
    [deprecation_warning] => next_max_id and min_id are deprecated for this endpoint; use min_tag_id and max_tag_id instead
    )

    [meta] => Array
    (
    [code] => 200
    )

    [data] => Array
    (
    )

    )

  5. It is a real problem of the API. You need your application out of the Sandbox state and do it alone hiring intermediaries so they can rid your app.
    Says clearly Instagram:

    “This use case is not supported. We do not approve the public_content permission for one-off projects such as displaying hashtag based content on your website. As alternative solution, you can show your own Instagram content, or find a company that offers this type of service (content discover, moderation, and display).

    You can find more information in the Permission Review documentation.”

  6. Instagram recently started showing view counts on videos. Is there any way to pull this data from the API?

    I checked some mobile IOS APP’s that provide total video view counts

    for example: IOS Mobile APP 6000 Followers

  7. how to get hashtag from Instagram ..I need to grape another user command and get user profile pic as well as command ….

    please tell me how its possible

    i am successfully fetch all my profile records here..but try to fetch another user records

    please help me

  8. error come “Array ( [meta] => Array ( [error_type] => OAuthAccessTokenException [code] => 400 [error_message] => The access_token provided is invalid. ) )”
    can any one explain me what happen

  9. Array ( [meta] => Array ( [error_message] => The access_token provided is invalid. [error_type] => OAuthAccessTokenException [code] => 400 ) )

  10. The error message says “The access_token provided is invalid”. To get rid of that you have to uncheck “Disable implicit OAuth” in the Security tab of your client.

Comments are closed.