Create your own Instagram app with PHP

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.

 

  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' => '',
'client_secret' => '',
'grant_type' => 'authorization_code',
'redirect_uri' => 'http://your.call/back/url.php',
);

If you have any question, please use the Github Project Issue Tracker.

25 thoughts on “Create your own Instagram app with PHP

  1. lorenzo

    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?

    Reply
  2. Mauricio Cuenca Post author

    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.

    Reply
  3. PaulMighty

    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!

    Reply
  4. Lorenzo

    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

    Reply
  5. Mauricio Cuenca Post author

    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.

    Reply
  6. Caterpuchi

    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!

    Reply
  7. Andrew

    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!

    Reply
  8. Andrew

    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!

    Reply
  9. Zsolt Takács

    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.

    Reply
  10. Shane

    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.

    Reply
  11. Dan

    Hi Mauricio,
    at first: thank you so much for developing and sharing this library ;)

    I’m stumbling while I try to “read” a whole hashtag-stream. That means that I’m interested in data from every picture within a defined #hashtag.
    The actual number of pictures are about 240 (later on that number will get bigger ;)
    Because the API provides only 20 (with the parameter count 60) I’ve to handle that pagination-thing. But I’ve no idea how to handle that.
    Can you give me any example or hint?

    Reply
  12. mistered

    Hi Mauricio,

    Could you provide us an exemple to use the getUserRecent function.

    I try $popular = $instagram->getUserRecent(1574083);

    With no success.

    I’ll apreciate your answer.

    Thx!

    Reply
  13. jakenow

    Mauricio how do we get in touch with you??? i want to hire you for a job. could you send me an email? i entered it in the reply box says it wont be published but u should be able to see it

    Reply
  14. Max

    Hi Guys,

    still reading all of the comments, but i still dont get rid of the problem at the callback.php

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

    Anyone know whats the problem? It would be a pleasure to know! Thanks at all! <3

    Reply
  15. Mark Dhas

    Hi Mauricio,

    I’ve got a smallish problem here, I’ve set it up the script and have a $config file and am requesting the getRecentTags method. Tracing through the logic I’ve arrived at Instagram->getAccessCode() which is supposed to return $_GET['code']. However the page that is firing this code doesn’t have code as a $_GET parameter. Having traced the logic through to that point I know that upon instantiation. It then goes the getAccessToken() method, which then hands of to setOauthToken() that then calls initHttpClient calling the Instagram with the following options POST with client_id, client_secret, grant_type, redirect_uri and code but code is supposed to be $_GET['code']).

    From what I can discern up until we get to this point there has been no communication with the Instagram API server, so where is $_GET['code'] coming from?

    Thanks in advance for all your help :)

    Cheers,
    Mark

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>