PHP insights, tips and tricks

Create your own Instagram app with PHP

  1. Register your application Instagram to get your OAuth credentials.
  2. Make sure to set your callback URL as a web address that is accessible anywhere from tje Internet, something like http://yourdoma.in/callback.php
  3. Download the latest version of the code in zip or tar.gz format.
  4. Unpack the code and modify the credentials stored in the $config variable of example.php to the ones obtained in step 1
  5. Point your browser to http://yourdomain/example.php. If the user is already authenticated, she must be presented with a recent popular photos screen. Otherwise, the authentication page will pop up.

This is the only part of the code that you should modify in example.php:

/**
* Configuration params, make sure to write exactly the ones
* instagram provide you at http://instagr.am/developer/
*/
$config = array(
    'client_id' => '<your_client_id>',
    'client_secret' => '<your_client_secret>',
    'grant_type' => 'authorization_code',
    'redirect_uri' => 'http://your.call/back/url.php',
);

If you have any question, don’t hesitate to post your comment and check back often, I read them frequently.

Tags: ,

17 Comments

rssComments RSS transmitTrackBack Identifier URI


Just testing your script with no success…
i modified all the things needed (both in script and in instagram conf)
but once the script authenticate and i got redirected to callback.php?code=…..
but the $instagram->getAccessToken() var is empty
so no data is pulled
and everytime i call example, i got to re-authorize

any idea?

Comment by lorenzo on June 8, 2011 12:37 pm


Hi Lorenzo, it sounds like you haven’t set session_autostart in your PHP environment. Try doing it or just calling session_start() at the very beginning of your php scripts. Tell me if it works.

Comment by Mauricio Cuenca on June 8, 2011 11:41 pm


Hey Mauricio, your script works great! Have deployed with ease, thanks for sharing with us. Was wondering if you were aware of a way to bypass the authentication by posting a username and password in xAuth style or something to that effect. Would love to be able to show my own feed on my website to visitors without requiring them to authenticate via their own Instagram username/password. Any thoughts?

http://web.stagram.com and http://www.instagre.at seem to be able to do this, but I haven’t figured out how they’re going about it.

Thanks for the hard work :) Cheers!

Comment by PaulMighty on August 8, 2011 11:59 pm


to be onest… im still having trouble with it
session autostart is on on my php,
when i authenticate on instagram, it redirect correctly, but the instagram object (not the session or cookie) is empty on:
[_arrayResponses:protected] =>
[_oauthToken:protected] =>
[_accessToken:protected] =>
[_currentUser:protected] =>
[_httpClient:protected] =>

you can see that here
http://www.popland.it/instagram/example.php

Comment by Lorenzo on October 25, 2011 12:15 pm


just to let you know… digging down to curl i added
curl_setopt($this->handler, CURLOPT_SSL_VERIFYPEER, false);
to have it working

Comment by Lorenzo on October 25, 2011 1:55 pm


its still me
despite it works the first time…
if i reload the page oauth lose everything…
and everything stop working
http://www.popland.it/instagram
it auth and show populars
if you reload… it breaks

Comment by Lorenzo on October 25, 2011 3:17 pm


Hello again Lorenzo, it seems that you’re not storing the token sent by the OAuth response. You need to save it and use it in all further requests, so you don’t have to do the handshake again. Try that and tell me if it works.

Comment by Mauricio Cuenca on October 26, 2011 10:54 pm


Hi Mauricio, you’ve done some excellent work on instaview.me, thanks a lot!

However, your basic script just won’t get to work for me. Whenever I try http://homofabe.scorpius.uberspace.de/example.php the authentification seems to work, but when coming back to my callback.php, there’s just this empty pic. Any clue?

Comment by Marcus Schwarze on November 6, 2011 10:13 pm


It’s not work at my web site.

Doesn’t it works any longer?

or need any other setting?

Comment by yome on November 23, 2011 9:51 am


I managed to get the script up, however when I do a refresh on the page, I get this error:

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

How do I resolve this so that everytime i refresh, the popular stream refreshes too? thank you!

Comment by Caterpuchi on November 24, 2011 6:48 pm


Hello! I’ve been trying to work on this for about a week now and I’m having difficulties getting passed obtaining the ?code={code}.

I’ve done what you wrote on your second post. I had added my callback URL as mysite.com/Instagram.php. I also added that into the callback.php array and example.php array, but nothing is displaying when I get redirected with the code= value in my URL. I believe I’m having the same problem as Lorenzo, but I tried putting in the curl he posted under function _setOptions() but I’m still getting nothing.

Thanks for your time!

Comment by Andrew on December 9, 2011 5:19 am


Okay, I’ve got it to work, and I can output json of data. But its telling me that there is non-object?

Comment by Andrew on December 9, 2011 6:05 am


Okay, sorry for all these comments. but I got it to work. Now, I’m having another problem like Lorenzo where if you refresh, or if you reload the page another time, I get a an error where its asking for the access_token again. And I notice you said that you need to store it? I’m familiar with mysql and the database where I can insert into my database, but when I insert it into my database, how do I call it back so I don’t have to get the access token again?

Thank you!

Comment by Andrew on December 9, 2011 9:00 am


Nevermind! I figured it out! hehehehhe thank you!!

Comment by Andrew on December 9, 2011 9:02 am


Hello!

I think it’s a great lib, but i got some bugs and problems. I found a bug in the modifyUserRelationship($id, $action) method, there was a Zend framework script, I fixed to CurlHttpClient::POST.

But, I can GET anything, names, relationships etc, but when I want to modify the relationship between the current user and a fix user, the request has sent, but I got nothing to results… Why? Is the Instagr.am check my IP or geolocation and bans me?

The modify method doesn’t work.

Please help!

Regs.

Comment by Zsolt Takács on December 9, 2011 1:57 pm


will you be updating your examples with real-time updates? :)

Comment by Andrew on December 10, 2011 9:54 pm


Andrew, can you help me? Having same problem. I need to store access_token to database and recall it if page is refreshed/reloaded. Can you provide some code examples on how you’re handling this? Thanks.

Comment by Shane on January 9, 2012 12:17 am

addLeave a comment