CodeIgniter Instagram API Library Released

After using Instagram for a while I began looking at their API and then saw that there is no CodeIgniter library for it so I began making one.

You can download it from

And see a sample demonstration at

Use and enjoy.

I’d love to see any projects that use the library.

This entry was posted in Uncategorized. Bookmark the permalink.

45 Responses to CodeIgniter Instagram API Library Released

  1. Fuzen Design says:

    The sample demo link isn’t working.

  2. ian says:

    Thanks for letting me know. It was working from the home page but not on the actual post.

    It is working from both pages now.

  3. I just try to use getPopularMedia(); but there’s doesn’t show anything.

    Would you explain me how to use it?

  4. ian says:

    Hi Gage,

    The following code should give you an idea on how to use the getPopularMedia() function. Create a controller file called popular.php and paste the following into it.


    // Get the popular media
    $popular_media = $this->instagram_api->getPopularMedia();

    // Loop through the data returned by Instagram
    foreach($popular_media->data as $data) {

    // To see all the data that each media item contains uncomment the following
    /*echo '<pre>';
    echo '</pre>';*/

    // Display the thumbnail image
    echo 'images->thumbnail->url . '" />';

    // Display the user name
    echo 'Taken by ' . $data->user->username . '';

    echo '<hr />';




    The function returns a lot of data about each popular media item. Once it is assigned to a variable you can loop through and pick out the parts you want to display.

    To see all the data available for each media item uncomment the print_r statement.

    There is data about comments, filter used, more data about the user, location and a few more that you will be able to see.

    Hope that helps.

    Let me know if you have anymore questions.


  5. Hi Ian,
    Thanks for replying my comment.

    Yes, i was did it:

    function index()

    $popular_media = $this->instagram_api->getPopularMedia();

    foreach($popular_media->data as $data) {

    and I got this message from CI:

    “Message: Trying to get property of non-object”

    Is there something wrong with the library?

  6. Hi Ian,

    I got the problem!
    The app not work on local machine.

    It’s work now!
    Thanks man..

  7. ian says:

    Glad you got it working.

    I developed it on a live server as all the other functions require you to login with Instagram which then uses a callback to a URL you have stated. This is how you get the OAuth token. So I never tried it on a local machine myself.

    Let me know when you have developed something I’d love to see what the library is being used for.

  8. Rob Pomeroy says:

    Ian, I’m so glad you created this library – you’ve saved me the job! 😉 Just one thing that would be really useful is example OAuth code. Armed with that one example, it should be relatively trivial to work everything else out.

    If I can think of anything useful to contribute, I’ll do so via Git. Also, if my fledgling app turns into anything interesting, I’ll let you know.



    PS You should put a reference to this library on the CodeIgniter wiki!

  9. Rob Pomeroy says:

    Okay, I worked out the OAuth stuff – I’m guessing your intention is that we manually set the public variable ($access_token) within the class. Perhaps this should be done automatically with some intelligent parsing within the __apiCall() function? Or at least with a setVariable() type function. What do you think?

    Once I’m happy with my code, I’ll either contribute back to Github (unlikely I’ll be that happy!) or post something on the CI forum (more likely).



  10. ian says:

    Hi Rob,

    Thanks for letting me know you are using and enjoying the code.

    With the OAuth there is a call for popular media that does not require OAuth which is one of the reasons I kept it a bit more manual.

    It also lets developers store it as they would prefer. E.g. in a session variable or database table.

    I look forward to hearing of any projects you use it in.



  11. Rob Pomeroy says:

    I’ve added some notes to the forum page on how I’ve handled authentication .


  12. Rob Pomeroy says:


    I think you may need to take another look at the API URLs. Where you’re using %d, empty parameters (NULLs) are being cast into integers (0). So I end up with a call like:

    The zeroes cause Instagram to return no data, just a 200 success code. Swapping to %s throughout gives correct results – personally, I’d switch to strings, since it’s a URL we’re sending ultimately. Relying on implicit casting can cause unexpected results (as above). Not sure if this behaviour (casting NULL to 0) is a change in PHP, by the way. I’m on 5.3.



  13. Rob Pomeroy says:


    I think you may need to take another look at the API URLs. Where you\’re using %d, empty parameters (NULLs) are being cast into integers (0). So I end up with a call like:

    The zeroes cause Instagram to return no data, just a 200 success code. Swapping to %s throughout gives correct results – personally, I\’d switch to strings, since it\’s a URL we\’re sending ultimately. Relying on implicit casting can cause unexpected results (as above). Not sure if this behaviour (casting NULL to 0) is a change in PHP, by the way. I\’m on 5.3.



  14. Marcel says:


    i love your library. Thank you for that.
    I was just about to write you an email, because of my problem by receiving no data with the function getUserFeed() e.g. But now, as i read the comments, i’d like to thank Rob for his post. You saved me a few hours of work. Brilliant.

    Thank you both, you rock.
    Keep on doing such a good Work.



  15. ian says:

    Thanks for letting me know Rob,

    I’m just looking at the API results at the moment to see if simply switching to %s is a good enough fix or if will take something more.



  16. ian says:

    Hi Rob,

    I have pushed the update to GitHub and included a credit to you in the commit.



  17. ian says:

    Hi Marcel,

    Thanks for letting me know. I have just pushed an update to GitHub.

    Please feel free to share your work with the library by posting a link here.



  18. Marcel says:

    Hi Ian,

    i’ll let you know when its done.

  19. One thing I noticed lacking, or I can’t find is support for the “next_url” pagination. Currently if I call the locationRecent it will return me the first response with 18 pictures. What if I want all the pictures from said location?

  20. ian says:

    You can pass in a max id as the second parameter and this will return media after that id.

    You could use this to get the next set of media.

  21. Thanks for writing wonderful library ,
    how to logout from instagram?

    Muhammad Hasan Samee
    Zend PHP 5.3 Certified Engineer

  22. ian says:

    There is currently no API Endpoint to logout of Instagram.

    One way to log users out from Instagram as well as your app is to add an iframe to the logout page like this

  23. vinny says:

    any update for postMediaComment

  24. ian says:

    Hi Vinny,

    I’ve updated the library on Github and the post media comment function is now working.

    It needs a media id and a comment passed to the function like this.

    $this->instagram_api->postMediaComment('a-media-id-123456', "I'm testing posting comments from my CodeIgniter library");

  25. spid3y says:

    hello ian,
    when I download this library and try in my computer, i have some problem,
    when I wan login with instagram, I found this message
    The requested URL /login/demonstrations/instagram-library/user/profile was not found on this server.

  26. ian says:

    It sounds like you haven’t got a controller and corresponding function setup on your install of CodeIgniter.

    The library is just a way of interacting with the Instagram API rather than setting urls in your application.

  27. sari yono says:

    hi ian im a big fan of your library i tried using the sample app you have out there for some reason im not getting the access_token to be added to the session data matter of fact i think its not storing anything from the authorization

    thanks again great work my friend

  28. ian says:

    In the file application/controllers/authorize.php on lines 34 and 40 are redirects.

    If you comment these out and then add in some code to show what is set in the page this is a good starting point.

    Once you find out if you are getting a code in the url or not let me know and I can try and help you out from there.


  29. Rory says:

    Hey Ian,

    So I’m messing around with that sample app. I changed it so that instead of going to the welcome.php, it goes to tags.php and looks for recent tags. However when I run the script I get this error:

    Message: Undefined property: stdClass::$data

    Filename: views/tags_recent.php

    Line Number: 3

    I’ve tried a couple different things and I just can’t seem to figure it out. Any help would be great.


    P.S. I might have sent this twice by mistake

  30. ian says:

    Hi Rory,

    Before being able to request images by recent tags you first need to login.

    I haven’t handled this very elegantly in the sample app.

    The call to the recent tags API function from Instagram requires an authentication token which you gain from logging in.

    If you need more help with this part please let me know.



  31. Rory says:

    So will I have to call the Login function in the library, or do I use my client ID as the authentication token?

  32. ian says:

    You’ll need to use the login ffunction.

    A user is sent to the Instagram login page with your details appended to the url, such as callback url.

    When they successfully login on the Instagram page they are redirected to the callback url that is supplied.

    An authentication token is appended to this callback url. The demo application then stores this as a session variable.

    This allows the authentication token to the be sent with all necessary calls to the API.

  33. Rory says:

    Hey Again,

    Sorry I am still having trouble with this. I have the login function in the application and that works okay, but the tags aren’t working. Its almost like the application is not recognizing that I am now logged in. However I am noticing that the url is giving me code. Is that what I need?

  34. Rory says:

    I think what the problem is that is not calling the authorize function in this demo because even the login on the welcome isn’t working. Where do I need to place the authorize function?

  35. ifew says:

    How to get more likes and followers on Instagram?

    This is a clone writing with PHP + Codeigniter

  36. Richard Monzon says:

    When I use the getUser function, I get an error message;
    public ‘error_message’ => string ‘Missing client_id or access_token URL parameter.’ (length=48)

    Do I replace %s with something?

    When using the instagramLogin function, I noticed there’s no param to pass the scope, is there someplace else that I would enter the scope?

  37. ian says:

    Hi Richard,

    Thanks for letting me know.

    I have updated the code to include a scope parameter in the authentication process.

    This can be set in the config file.

    For the missing client id have you checked that it is being stored correctly after being redirected back to your application?

  38. Neal Piche says:

    the stated method for logging out has been blocked by the petty losers at instagram, who make an api and then bend you over, etc.

    Anyway do you know of a working method now to logout?

  39. ian says:

    There are a couple of suggestions on I haven’t tried the ajax and img src ones so can’t say whether they work or not.

    I suppose the solution that would work is to just link to the log out page on Instagram. The downside is it takes a user away from your app.

  40. TPT says:

    how instagram share button work in codeigniter

  41. ian says:


    I haven’t got round to looking at the like button yet.

    This post might prove helpful



  42. ian says:

    Instagram apps are in sandbox mode when you first create them. This is to allow for testing before submitting your app for review.

    This page details the restrictions when in sandbox mode, e.g. number of users

  43. Hi ian

    will you please let me know that how can i get authorize user detail……..
    because in your function $this->instagram_api->get_user($user_id); need a user_id

  44. ian says:


    In the demo app I store the user id in a session variable during the authorize process

    If you need to find a user id other than the current one you could try implementing the /user/search method which is not currently in the library.



Leave a Reply

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