Instagram API How-to

Before you start, drugs 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, neurologist 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 . . .

https://api.instagram.com/v1/

. . . then find the endpoint for the type of data you’re requesting — popular photos, approved 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:

https://api.instagram.com/v1/media/search?

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:

https://api.instagram.com/v1/media/search?lat=48.858844&lng=2.294351&access_token=ACCESS-TOKEN

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:

https://api.instagram.com/v1/media/search?lat=48.858844&lng=2.294351&client_id=MY-CLIENT-ID

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:

$json_array['data'][0]['link']

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>