Note. This post is almost 10 years old now. As pointed out in the comments by Jan-Stefan Janetzky, it seems like Instagram have started to invalidate tokens more frequently now. I will not update this blog post anymore since I don't use PHP on a day to day basis anymore.
Did a recent project at work where the client wanted to show his Instagram feed on his page. It was a WordPress project (not a big fan of WordPress or PHP...) so Yes, I could've used a plugin to do it, but how fun is that?
Decided to build my own awesome implementation and share it here. It's nothing special really, just some curl requests against the Instagram API.
Let's get started, first you will need to register your application on the Instagram dev site and obtain your Client ID. Set the callback url to localhost or whatever(not important, you will see why below).
Now let's obtain the access token.
Paste this in your browser(replace client_id with your newly created one):
https://instagram.com/oauth/authorize/?client_id=CLIENT_ID_GOES_HERE&redirect_uri=http://localhost&response_type=token
Grant the application access to your images and press OK.
You will now recieve a 404 page or something similar, that's ok though, look in the adressbar right after the #...aah that's right, that's your access token, pretty sweet huh?
Copy and save your access token, you will need it to "sign" all of your requests against the API.
Now let's make some API calls! Our client wanted to display their timeline so we will do the same thing.
To get the timeline of a user from the API the request need to look like this(19151466 is my Instagram ID):
/v1/users/19151466/media/recent?access_token=OUR_ACCESS_TOKEN
Here comes some simple PHP code where we are using CURL to retrieve the JSON-data from the API:
$url = "https://api.instagram.com/v1/users/19151466/media/recent?access_token=OUR_ACCESS_TOKEN
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$json = curl_exec($ch);
curl_close($ch);
The response will look something like this:
Happy json parsing!