| NOTICE: Thanks to everyone interested in this. Sadly, I haven’t had the time that I would like to dedicate to this blog lately. I invite you to head directly to the Github Project Issue Tracker where a more fluid discussion can take place and a bigger community can participate and help with your particular issues. |
Instagram recently released an official API. Initially, they had implementations in Python and Ruby, so I decided to build mine in PHP with a little help from Zend Framework.
In order to use the official API, you have to sign up first for a client key at http://instagr.am/developer/. You will need to register a new client or use the data from an existing one, just keep in mind these are the important data that you need before you can interact with the API:
Client Id: let’s call it a public key. It is necessary so the API knows your application. This is public available information, for the purpose of the example provided with my implementation we are going to query Instagram’s service directly using the client key as part of the URL.
Client secret: this is the private key that once paired with the client id is exchanged with Instagram’s service. The client secret needs to be private, but you have to make it available in some form inside your code so it can be sent and to get the authentication token back.
Callback url: maybe on of the most important aspects in this example. Make sure the callback URL is exactly the same where you are going to put the script that acts as an entry point for the Instagram callback. For this particular example, this script is going to be called “instagram.php”, meaning that you’ll have to point Instagram to something like http://example.com/instagram.php.
So this is as easy as one, two, three.
1. Grab the code from GitHub in you preferred format: zip or tar.gz and Unpack it in the same folder you told Instagram your callback was going to live.
2. The example we create in this post, authenticates and displays the most popular photos according to the data sent back by the API. The implementation provides access to all public methods, so you can create you own stuff. Amongst others, you can search for media, perform ‘follow’ actions or search media by location.
3. Just make sure to modify the code, so you can put your own client properties in the $config array at the beginning of the file:
/**
* Configuration params, make sure to write exactly the ones
* instagram provide you at http://instagr.am/developer/
*/
$config = array(
'site_url' => 'https://api.instagram.com/oauth/access_token',
'client_id' => '', // Your client id
'client_secret' => '', // Your client secret
'grant_type' => 'authorization_code',
'redirect_uri' => '', // The redirect URI you provided when signed up for the service
);
And now, the code of the instagram.php script:
require_once 'Instagram.php';
/**
* Configuration params, make sure to write exactly the ones
* instagram provide you at http://instagr.am/developer/
*/
$config = array(
'site_url' => 'https://api.instagram.com/oauth/access_token',
'client_id' => '', // Your client id
'client_secret' => '', // Your client secret
'grant_type' => 'authorization_code',
'redirect_uri' => '', // The redirect URI you provided when signed up for the service
);
// Instantiate the API handler object
$instagram = new Instagram($config);
$popular = $instagram->getPopularMedia();
// After getting the response, let's iterate the payload
echo "
<ul>\n";
$response = json_decode($popular, true);
foreach ($response['data'] as $data) {
$link = $data['link'];
$caption = $data['caption']['text'];
$author = $data['caption']['from']['username'];
$thumbnail = $data['images']['thumbnail']['url'];
?>
<li><a href="<?= $link ?>"><img title="<?= $caption ?>" src="<?= $thumbnail ?>" border="0" alt="" width="150" height="150" align="absmiddle" /></a> by <!--?= $author ?--></li>
<!--?<br /--> }
echo "</ul>
\n";
Finally, point you browser to the authorization URL and enjoy your coding!
https://api.instagram.com/oauth/authorize/?client_id=CLIENT_ID&redirect_uri=REDIRECT_URL.
You can get more details about manipulating the API at http://instagr.am/developer/