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.

45 thoughts on “CodeIgniter Instagram API Library Released”

  1. 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.

  2. 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.


  3. 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?

  4. 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.

  5. 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!

  6. 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).



  7. 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.



  8. Ian,

    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.



  9. Ian,

    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.



  10. Ian,

    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.



  11. 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.



  12. 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.



  13. 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?

  14. 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.

  15. 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

  16. 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");

  17. 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.

  18. 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.

  19. 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

  20. 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.


  21. 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

  22. 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.



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

  24. 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.

  25. 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?

  26. 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?

  27. 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?

  28. 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?

  29. 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?

  30. 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

Leave a Reply

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