Tag Archives: cookies

Cookies! Nom!

The Instagram API works in a very similar way to Foursquare’s API. You start with a base URL, weight loss help “https://api.instagram.com/v1/” then find the endpoint for the type of data you’re requesting — popular photos, seek photos with a particular tag, etc. Find the details of the endpoints available here: http://instagram.com/developer/endpoints/
For media, the endpoint starts with “media” and then has a second element depending 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/popular?”. Everything after the question mark is where we put the custom parameters, like the location to search within. The available parameters are documented here: http://instagram.com/developer/endpoints/media/#get_media_search Looking at the available parameters for 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.

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 looking at the documentation, you’ll see 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 a log in as a user and change stuff, like add a tag or comment.) So, your 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 that into the browser, you should get a bunch of JSON text back. (Or a very short piece of JSON if there’s an error.) Also 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 data you might want in there — a bunch of links to jpgs, the username, the number of likes, and so on. Paste the text into http://www.jsoneditoronline.org/ to get a better look.

From there, 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']
In Tuesday’s class, viagra here I talked briefly about cookies, information pills but we didn’t have time to go over them in much detail. As promised, here’s some more information. First, I need to correct a mistake in the example code I demo’ed: Setting a cookie requires the built-in PHP function setcookie()¬†and can’t be done setting the $_COOKIE array directly. This explains why my example code was behaving strangely!

Here’s a short (~4 min) video on how to set and retrieve cookies in PHP:

The video talks about using the PHP time() function to set the expiration date of the cookie. The number used to represent time there is called a Unix timestamp, in case you’re curious.

Remember that you can inspect the cookies that any site has set in your browser by using the Chrome Web Developer tools. Scroll down to the Cookies section of this page for more info.