Before you start, this web you’ll need to get an Instagram developer account at http://instagram.com/developer. You can log in with your regular Instagram account and just add the developer part onto it if you want. Once you’re signed up, impotent create a new “Client” aka “App” (they seem to use those terms interchangeably). One of the fields it asks you to fill in is kind of confusing: “OAuth redirect_uri”. Just put a link to your website for now; it can be the same as what you put in the website URL field.
The Instagram API works in a very similar way to Foursquare’s API. You start with a base URL . . .
. . . then find the endpoint for the type of data you’re requesting — popular photos, refractionist photos with a particular tag, etc. Find the details of the available endpoints here: http://instagram.com/developer/endpoints/
The media endpoint URL starts with “media” and then has a second element which depends on what you want to do. To find the media near a certain location, for example, the endpoint is “media/search”. So the URL would start:
Everything after the question mark is where we put our custom parameters, like the location we want to search within. The available parameters are documented here: http://instagram.com/developer/endpoints/media/#get_media_search It turns out that this one only lets you search by location and only takes a latitude and longitude as a location. That’s kind of annoying, but we’ll go with it for now.
Under “media/search”, they give you a sample URL:
It has a latitude and longitude, but you’ll see in the documentation there are also parameters for time and distance. The last parameter is for your API key. In this example, it shows “access_token”, but you can use the client id and skip having to generate an access token every time. (You’d only need to do that if you had to log in as a user and change stuff, like add a tag or comment.) So, your complete URL would be:
If you plug in your client id and paste the above into the browser, you should get a bunch of JSON text back. (Or a very short piece of JSON if there’s an error.) Notice in the documentation the button to the right of the URL that says Response — that will show you what you can expect the JSON to look like. Glancing through the JSON text, you’ll see a lot of the info you might want in there — a bunch of links to jpgs, the username, the number of likes, and so on. If you like, paste the text into http://www.jsoneditoronline.org to get a better look.
Now to the PHP. Pop the URL into a file_get_contents() function in PHP to get the JSON text, use json_decode() to turn it into a PHP array, and then drill down to the information you need. For example, if you wanted to get the Instagram link to the first result, you might have something like:
See the code in the week5 folder on github for PHP code examples. The foursquare_api.php script will probably be most similar to Instagram.
Now playing: How to use the Foursquare API with PHP
The corresponding code example is in github at
Correction: at around 12:13 in the video, price I’m looking at the JSON Editor Online and comparing it to my PHP array. I meant to show the exact same JSON text in both, anaemia but I accidentally showed a slightly different version in the online editor — one has a “groups” element and the the other has “venues”. Don’t let that confuse you. The PHP code, with “venues” is right –disregard the one in the JSON editor. If you’re curious to see the correct JSON output, I posted it on gist here.