Instagram API: Fetch All Photos With a Specific Hashtag using PHP
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.
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 plateform.
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.
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.
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.
The next screen shows the success message with Client ID and Client Secret keys which we will use in our last step.
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.
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.