Instagram API implementation in PHP

Instagram is a fast, beautiful and powerful way to share your photos with friends and family. You can snap a picture, choose a filter to transform its look and feel, then post to Instagram. Also can share to Facebook, Twitter, and Tumblr. In this tutorial you will learn how to implement Instagram API using PHP.

Instagram API implementation in PHP

Recently Instagram released an official API that will be used with PHP.

In order to use the official API, you have to sign up first for a client key at You will also 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: it's 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

So this is as easy as one, two, three.

1. You can grab the code from GitHub in you preferred format at Instagram-PHP-API and Unpack it in the same folder you told Instagram your callback was going to live.

2. In this example, you authenticate 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:
$config = array(
'site_url' => "",
'client_id' => "", // Your client id
'client_secret' => "", // This Your client secret
'grant_type' => "authorization_code",
'redirect_uri' => "" // The redirect URI you provided when signed up for the service

And now the final code of the instagram.php script:

require_once 'Instagram.php’;
$config = array(
'site_url’ => "",
'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 src="<?= $thumbnail ?>" title="<?= $caption ?>" width="150" height="150" border="0" align="absmiddle"></a> by <?= $author ?></li>
echo "</ul>\n";<strong></strong>

and now point you browser to the authorization URL and see the result

You can get more details about manipulating the API at

You may like these posts