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 https://github.com/ianckc/CodeIgniter-Instagram-Library

And see a sample demonstration at http://ianluckraft.co.uk/demonstrations/instagram-library/welcome

Use and enjoy.

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

This entry was posted in Uncategorized. Bookmark the permalink.

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

    load->library('instagram_api');

    // 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>';
    print_r($data);
    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.

    Thanks

  5. Hi Ian,
    Thanks for replying my comment.

    Yes, i was did it:

    function index()
    {
    $this->load->library(‘instagram_api’);

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

    foreach($popular_media->data as $data) {
    print_r($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.

    Cheers,

    Rob

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

    Thanks,

    Rob

  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.

    Thanks

    Ian

  11. Rob Pomeroy says:

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

    Rob

  12. Rob Pomeroy says:

    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:

    https://api.instagram.com/v1/users/self/media/recent/?access_token=TOKEN&max_id=0&min_id=0&max_timestamp=0&min_timestamp=0

    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.

    Regards,

    Rob

  13. Rob Pomeroy says:

    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:

    https://api.instagram.com/v1/users/self/media/recent/?access_token=TOKEN&max_id=0&min_id=0&max_timestamp=0&min_timestamp=0

    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.

    Regards,

    Rob

  14. Marcel says:

    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.

    Regards,

    Marcel

  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.

    Thanks

    Ian

  16. ian says:

    Hi Rob,

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

    Thanks

    Ian

  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.

    Thanks

    Ian

  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.

    Thanks

  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.

    Thanks,
    Rory

    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.

    Thanks

    Ian

  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 autolikestagram.com clone writing with PHP + Codeigniter

    http://www.tosdn.com/developer/php/instagram-auto-like-picture-by-tag-php-robot-with-codeigniter-module/

  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?

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>

Security Code: