Recently, a friend of mine asked me a favor. She wanted to display her instagram feed on her personal website. Sure! Why not? Having experience with a similar task makes it easy for me to say yes. I thought it would only take about an hour or less, but I was wrong.
Pulling data from Instagram used to be easy using a public api as long as you are pulling data from a public user, a hashtag or even based on location. All you need to do is to append a query string (?_a=1) to and endpoint (eg: https://instagram.com/username/?_a=1) you can extract it using either cURL or file_get_contents() php function and viola, it will output a JSON where you can get data such as caption, post date, location, and image url that you can then display on your website.
Instagrams change to the Facebook API
It was all working smoothly on my local machine when I implemented it. But the moment I pushed it to the remote server, no output was being returned! So upon doing some research I found the issue, that Instagram discontinued and disabled all Legacy API Permissions (Basic Permission) and disallowed data scraping from June 29, 2020 (https://www.instagram.com/developer/).
So now what? Since Instagram already migrated from their Legacy API to the Facebook supported Graph API and app registration in Facebook is stricter, getting an API key is a challenge and will most likely take a lot of time. The good news is that Graph API has a public API using query_hash. A query_hash is Graph API’s way of reducing requests to the GraphQL server.
The concept of query_hash is the same as the Legacy’s query string. It will also return a JSON that you can extract using either cURL or file_get_contents() function. All you need to do is to attach the query_hash and its value to the Instagram endpoint depending on what data you want to get.
Example:
Get data from a username
query_hash for user = 472f257a40c653c64c666ce877d59d2b
To get your userid, use this web app https://www.instafollowers.co/find-instagram-user-id
https://www.instagram.com/graphql/query/?query_hash=472f257a40c653c64c666ce877d59d2b&variables={“id”:”user id here”,”first”:number of items,”after”:””}
Working Example
https://www.instagram.com/graphql/query/?query_hash=472f257a40c653c64c666ce877d59d2b&variables={“id”:”93024″,”first”:12,”after”:””}
Get Data from a hashtag
query_hash for hastag = 298b92c8d7cad703f7565aa892ede943
https://www.instagram.com/graphql/query/?query_hash=298b92c8d7cad703f7565aa892ede943&variables={“tag_name”:”your hashtag”,”first”:number of items,”after”:””}
Working Example
https://www.instagram.com/graphql/query/?query_hash=298b92c8d7cad703f7565aa892ede943&variables={“tag_name”:”random”,”first”:12,”after”:””}