PHP insights, tips and tricks

Instagram API implementation in PHP

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 src="<?= $thumbnail ?>" title="<?= $caption ?>" width="150" height="150" border="0" align="absmiddle"></a> by <?= $author ?></li>
<?
}
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/

Tags: , ,

31 Comments

rssComments RSS transmitTrackBack Identifier URI


[...] Librería PHP para acceder a la API de Instagram: Instagram un pequeño y gratuito programa de iPhone para aplicar efectos a fotos (y compartirlas tanto en su propia “red social” como en otras como Facebook o Twitter), que aunque apenas tiene web parece que si va a ser muy interesante para los developers. [...]

Pingback by Noticias 28-Febrero-2011 - La Web de Programación on February 28, 2011 1:02 am


Thanks Mauricio for doing this up & putting it on Github. I was just about to start work on one but figured I’d do a search first to see if someone else had already done it.

I’m looking into building a real-time photo printing service for Instagram - http://inkagram.com

What are you going to build?

Comment by Brent Shepherd on March 6, 2011 8:47 am


Thanks for passing by Brent, I just checked your idea and I loved it along with the design. I don’t have any plans right now on building a product based on my implementation. I just want to keep working on it and make it really robust so it can be the reference library for PHP developers who want to connect their project with Instagram. I’m the process of sending the proposal to Zend Framework so it can be part of their framework. I also have in mind creating a package for PEAR and building a standalone version that does not require any library apart from CURL.

Don’t hesitate to send me your comments and feedback regarding your experience with my code, I would be more than happy to help and build a solid library.

Comment by Mauricio Cuenca on March 6, 2011 1:23 pm


Hi Mauricio,
Thanks for the beautiful package. I have few questions regarding the package, perhaps you can enlighten me :)
I was reading the instagram api, there are 2 ways to connect the server

//explicit call
https://api.instagram.com/oauth/authorize/?client_id=CLIENT-ID&redirect_uri=REDIRECT-URI&response_type=code

//implicit call
https://instagram.com/oauth/authorize/?client_id=CLIENT-ID&redirect_uri=REDIRECT-URI&response_type=token

According to your code, I should try the token, which gives me blank screen (only returns me access_code). But, if I use the explicit call, it returns me all the images (with CODE string).

My question is, how to retain the access code through out the site?

I’m a novice, please bear with me.
Thanks,
Joko

Comment by Joko on March 16, 2011 10:43 pm


Hi Joko, if you grab the updated code from github, you’ll find two new methods I added so you can grab the access token upon the initial request and use it subsequently for all additional requests. According to Instagram’s documentation, the token doesn’t expire. Once you have the new code, you can do something like this:

// After the first request, you grab the access token
$instagram = new Instagram($config);
$popular = $instagram->getPopularMedia();
$accessToken = $instagram->getAccessToken();

// And then use it in every subsequent request
$instagram = new Instagram($config);
$instagram->setAccessToken($accessToken);
$popular = $instagram->getPopularMedia();

Comment by Mauricio Cuenca on March 16, 2011 11:51 pm


Mauricio,
you are my hero!
Thanks a bunch. I’ll download it and play around with it.

FYI: the zend library won’t work if I only download the Zend/Http… it throws me error after error. I fixed it by downloading whole Zend library.

I’ll post updates with my progress, if you don’t mind.

:)

Comment by Joko on March 17, 2011 12:35 am


Hey Mauricio,

the above example doesn’t work for me, any suggestions?

Here’s the message I get after executing:
$instagram = new Instagram($config);
$popular = $instagram->getPopularMedia();
var_dump($popular);

“{”meta”: {”error_type”: “OAuthParameterException”, “code”: 400, “error_message”: “\”client_id\” or \”access_token\” URL parameter missing. This OAuth request requires either a \”client_id\” or \”access_token\” URL parameter.”}}”

Comment by Thomas on March 28, 2011 3:53 pm


Hello Thomas, the example is pretty straightforward it should work right out of the box.
Do you have your code in the same address you set up as the “callback_uri” param? Looks like your trying to get the popular media from some other address. Also, make sure that you have cURL enabled in your server.

I’m about to release a new version of the implementation with a more detailed example. Stay tuned for the new update!

Comment by Mauricio Cuenca on March 28, 2011 9:33 pm


Don´t work you code:

Fatal error: require_once() [function.require]: Failed opening required ‘Zend/Loader.php’ (include_path=’.:/usr/local/php52/pear’) in /home/orlaspro/public_html/Zend/Http/Client.php on line 27

Seem to be missing files that are not included in the Zend folder. I even tried to restore the missing files and yet still fails the example

Comment by Fran Rodas on March 31, 2011 2:06 am


Seems pretty silly to have to download the whole Zend library, you should really mention that as a dependency in your blog post. It is totally unnecessary to include Zend Framework just for one library definition, in my opinion — surely there must be a way to free your script from that requirement…?

Comment by Marcy Sutton on April 4, 2011 6:05 am


Hello Mauricio,

I finally got the SDK working and I’m already using it for my Wordpress-Plugin Instapress.
I also extended the SDK for xAuth-Authentication, just let me know if you’re interested.

http://wordpress.org/extend/plugins/instapress/

Comment by Thomas on April 14, 2011 12:30 pm


@Marcy, The version that depended on ZF was the first one and I used it to deliver something fast. Now, I added a really simple HTTP client implementation based on cURL and there’s no need to download the Zend Framework library.

@Thomas, thanks for sharing instrapress. Looks really neat! My idea is to have something that covers a broader spectrum. I have done several changes to my classes since the last time you checked them. I’ll create another blog post explaining the new functionality.

Thanks!

Comment by Mauricio Cuenca on April 16, 2011 8:14 pm


@Fran: make sure to download the latest version from https://github.com/macuenca/Instagram-PHP-API
The Zend Framework dependency was removed, thus, your problem should be solved!

Comment by Mauricio Cuenca on April 23, 2011 5:18 pm


Hello Mauricio

Thanks for great work. somehow I get same error as everybody else.

{”meta”: {”error_type”: “OAuthParameterException”, “code”: 400, “error_message”: “\”client_id\” or \”access_token\” URL parameter missing. This OAuth request requires either a \”client_id\” or \”access_token\” URL parameter.”}}

I tried on 2 different server, medial temple and bluhost. CURL is working on both, same issues. Hmm what am I doing wrong? I just downloaded latest from githum.com as well!!

Comment by Toki on April 26, 2011 3:40 pm


Hello Mauricio

I posted previous post, and somehow I got it working by adding “display=touch” into $_endpointUrls['authorize'].

Just like this.
‘authorize’ => ‘https://api.instagram.com/oauth/authorize/?display=touch&client_id=%s&redirect_uri=%s&response_type=%s’

Hope it helps you and everybody else using your code.

Thanks

Comment by Toki on April 26, 2011 4:14 pm


This is wonderful and I truly appreciate your initiative. Unfortunately I am getting an error Warning: Invalid argument supplied for foreach() in callback.php on line 55. Any ideas would be mucho appreciated.

Comment by undaunted on April 28, 2011 2:22 am


Hello Mauricio,

Thanks for wonderful package, I have a doubt : I would also like to clarify that we would like the option to change (the code) what tag Instagram users attach to each photograph they upload. And not just location. For example, if we chose the word #blue all users that tag their photo with #blue will appear in real-time.
and like Instagram demo site generate dynamic pictures randomly. How could i do it on php ?

Comment by Pandav on May 28, 2011 10:10 am


Hello Pandav, there are three different methods to kind of accomplish what I guess you’re looking for:

You can use getTags($tagName), getRecentTags($tagName) or searchTags($tagName). Each method is self explanatory, the code also mentions the purpose of each one.

Keep in mind that my code doesn’t interact with the real-time API, just the regular one. But feel free to modify the code and adapt it so it accomplishes this purpose, should be somewhat easy to do.

Comment by Mauricio Cuenca on May 28, 2011 12:39 pm


Guys, try this for authentication;

https://api.instagram.com/oauth/authorize/?client_id=CLIENT-ID&redirect_uri=REDIRECT-URI&response_type=code

then everything will be OK :)

Comment by smlnl on June 2, 2011 1:35 am


Guys, try this for authentication;

https://api.instagram.com/oauth/authorize/?client_id=CLIENT-ID&redirect_uri=REDIRECT-URI&response_type=code

then everything will be OK

Comment by smlnl on June 2, 2011 1:35 am


Hi,
thanks for this - it’s always nice to pick up a library to help.

One question - how do you handle pagination? Looking at the API docs it’s as if you need to deal with the “next_url” parameter fed back in results?

Joel

p.s. good work!

p.p.s the api allows you to pass “SELF” in to getUser; I think you assume it’s an int

p.p.p.s getUserRecent (and, I guess, others) has a “count” parameter as well (restricting number of results coming back) handy!

Comment by @Joel_Hughes on June 24, 2011 2:01 pm


Hi Mauricio,

thanks for your work but still receive the following error when I try the example.php script.

I put the Client ID and secret and redirect URI as received from instagram develop site.

Warning: Invalid argument supplied for foreach() in /home/………../callback.php on line 55

Any help?

Gracias!

Comment by giotis on September 9, 2011 4:30 pm


[...] is based on Mauricio Cuenca’s PHP SDK [...]

Pingback by Instapress | WordPress Designer on September 14, 2011 9:17 am


i can upload it.. but the post can’t share on twitter..

Comment by eda on November 6, 2011 5:32 pm


Hi,

Thanks for this super script, but i can’t Get the current user id, i use getCurrentUser(); but i have no result…

Do you have any idea ?

Cheers

Comment by Ludo on November 14, 2011 6:39 pm


@Mauricio I have same warning but on a different line when its time to load photos. Cant figure it out. So annoyed. Anyone know why this happens from time to time?

Warning: Invalid argument supplied for foreach() in

Comment by lisa on November 16, 2011 6:16 am


Thanks your code works fine!

Could someone help me out with the following:

Iam trying to implemt this code in to my cms. To allow users to publish there photos on there website.

So Iam trying to get the username of that user so my cms can list there photos instead of most popular photos.

Is that even possibe? In the “example.php” there are asked to login. Then they are redirected to the defined url. There an id shoud be found?
Anyone?

Anyonee

Thanks

Comment by Ralph on November 28, 2011 3:55 pm


Iam a little bit futher..

$userdata=$instagram->getUser($_SESSION['InstagramAccessToken']);
$userdata = json_decode($userdata, true);
//shows fullname
print “full_name=”.$userdata['data']['full_name'].”";

But underneath function doesnt require a user id?

public function getUserFeed($maxId = null, $minId = null) {
$endpointUrl = sprintf($this->_endpointUrls['user_feed'], $this->getAccessToken(), $maxId, $minId);
$this->_initHttpClient($endpointUrl);
return $this->_getHttpClientResponse();
}

Comment by Ralph on November 28, 2011 7:08 pm


Hi,

Thanks for your script. That help me a lot but….. :-) I got a problem for modify users relationship.

I use : $followtest =$instagram->modifyUserRelationship(an_id,’follow’); but i got this message :”APIInvalidParametersError [code] => 400 [error_message] => please supply action=approve,ignore,follow,block,unblock,unfollow )”

Thanks for your help with advance…

Guilb

Comment by Guilb on January 17, 2012 1:52 am


So Iam trying to get the username of that user so my cms can list there photos instead of most popular photos.

Is that even possibe? In the “example.php” there are asked to login. Then they are redirected to the defined url. There an id shoud be found?
Anyone?

Comment by pet supplies plus on January 19, 2012 7:41 am


How would one use this to just show images on a page. It seems that the authorization URL is required everytime to show anything. I would just like to display my images on a page. Thanks.

Comment by Greg Thompson on January 23, 2012 9:54 pm

addLeave a comment