EllisLab text mark
Advanced Search
     
Instagram API Library
Posted: 07 May 2011 11:47 AM
Joined: 2010-01-20
34 posts

I have made a library for interacting with the Instagram API.

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.

Thanks

 
Posted: 31 May 2011 08:17 PM   [ # 1 ]   [ Rating: 0 ]
Avatar
Joined: 2002-10-02
103 posts

Looks interesting. Thanks for sharing this.

 Signature 

———-
work: jmx2.com
blog: SuperGeekery
twitter: johnmorton

 
Posted: 21 December 2011 12:38 PM   [ # 2 ]   [ Rating: 0 ]
Avatar
Joined: 2010-08-10
18 posts

Wrapping your head around OAuth authentication is the hardest part to using this (or any similar) library, I think.  I’ve taken the approach of storing the OAuth token in session tables and adding all my logon checking to my controller’s constructor.  E.g. (CI 2.1.0):

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class 
extends CI_Controller {

  
/**
   * Instagram_api object used in all controllers
   * @var object
   */
  
protected $_ig;

  
/**
   * Whether or not we've got a valid oAuth Instagram token
   * @var bool
   */
  
protected $_valid_token;

  
/**
   * User data from Instagram (if we're logged in)
   * @var stdClass Object
   */
  
protected $_ig_user;

  public function 
__construct() {
    parent
::__construct();

  
// Set the callback path to the current URL - we always return to whatever page we were on
  
$this->config->set_item('instagram_callback_url',
  
$this->config->item('instagram_website').$this->uri->uri_string());

  
// Get an Instagram_api object for use throughout this controller.
  
$this->_ig = new Instagram_api();

  
// Retrieve all session data
  
$sess_data $this->session->all_userdata();

  
// Initialise token status to FALSE (i.e. we're don't know if we're logged in yet)
  
$this->_valid_token FALSE;

  
// Check if we have an oAuth token in session data
  
if (isset($sess_data['ig_token']) && isset($sess_data['ig_userid'])) {

   
// We have an oAuth token - might not still be valid though
   
$this->_ig->access_token $sess_data['ig_token']// not ideal but that's how this class works at the moment

   // Retrieve our own data to see if the token's valid
   
$user $this->_ig->getUser($sess_data['ig_userid']);
   if(
$user->meta->code == 200{
    
// We're logged in
    
$this->_ig_user $user->data;
    
$this->_valid_token TRUE;
   
}
  }

  
if(!$this->_valid_token{
   
// No token, but we may have a code and therefore be in the middle of authenticating
   
$code $this->input->get('code'TRUE);
   if (
$code != ''{
    $oauth 
$this->_ig->authorize($code);
    
/* Successful OAuth:
       [access_token] => [a string]
       [user] => stdClass Object
         (
           [username] => name
           [bio] => blurb
           [website] => url
           [profile_picture] => iamge url
           [full_name] => Name
           [id] => id
         )

      Else (eg):
       [code] => 400
       [error_type] => OAuthException or OAuthAccessTokenError
       [error_message] => No matching code found or some other message
     */

    
if(isset($oauth->access_token)) {

     
// We've logged in okay
     
$this->_valid_token TRUE;

     
//  Store access token in session and Instagram_object
     
$this->session->set_userdata('ig_token'$oauth->access_token);
     
$this->_ig->access_token $oauth->access_token// not ideal but that's how this class works at the moment

     // Store Instagram userid in session
     
$this->session->set_userdata('ig_userid'$oauth->user->id);

     
// Grab Instagram user data
     
$user $this->_ig->getUser($oauth->user->id);
     if(
$user->meta->code == 200{
      $this
->_ig_user $user->data;
     
else {
      
// Some weird error has occurred
      // TODO: handle it!
     
}

    } 
elseif(isset($oauth->error_type)) {

     
// TODO: handle being rejected
     
echo $oauth->error_type;
     echo (isset(
$oauth->error_message) ? ': '.$oauth->error_message '');

    
else {

     
// TODO: handle unknown failure
     
echo 'Unspecified error.  Barfing.';
    
}
   }

  }

  }


 
public function index() {

  
if($this->_valid_token{
   
echo 'logged in as: </br>';
   
pre_print($this->_ig_user);

  
else {
   $auth_url 
$this->instagram_api->instagramLogin().
    
'&scope=comments+relationships+likes';

   echo 
"<a href='$auth_url'>login</a><br />";
   echo 
"You need to ensure cookies are enabled in your browser before attempting to log on.";

  
}

  
// Whether logged in or not, we can get the popular media
  
$popular_media $this->instagram_api->getPopularMedia();
  
pre_print($popular_media);
  foreach(
$popular_media->data as $item{
    
echo "<a href='{$item->link}'><img src='{$item->images->thumbnail->url}' /></a> ";
  
}

 }

 
public function test()
 
{
  
if($this->_valid_token{

   
echo 'do other stuff here';

  
else {

   $auth_url 
$this->instagram_api->instagramLogin().
    
'&scope=comments+relationships+likes';

   echo 
"<a href='$auth_url'>login</a><br />";
   echo 
"You need to ensure cookies are enabled in your browser before attempting to log on.";

  
}

 }

}

/* End of file p.php */
/* Location: ./application/controllers/p.php */ 

It’s a bit messy, but I offer this in the hopes it will get someone started a bit quicker.

Note that in my application, most pages require authenticated IG access.  Every controller that follows the above model can handle the authentication itself - i.e. you don’t need a dedicated logon page.  To achieve that, ensure that the callback URL configured at the Instagram end is set to the base of your web site.

 Signature 

Author of the fantasy thriller, Insensate - available for all ebook readers and iDevices. Find out more here: . Only 99¢!

 
Posted: 26 April 2012 05:48 PM   [ # 3 ]   [ Rating: 0 ]
Joined: 2012-04-26
2 posts

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

 
Posted: 27 April 2012 04:04 AM   [ # 4 ]   [ Rating: 0 ]
Avatar
Joined: 2010-08-10
18 posts
Muhammad Hasan Same - 26 April 2012 05:48 PM

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

That would depend on how your application is storing login details.  In my case, I’m using sessions, so logging out is as simple as:

$this->session->sess_destroy(); 
 Signature 

Author of the fantasy thriller, Insensate - available for all ebook readers and iDevices. Find out more here: . Only 99¢!

 
Posted: 27 April 2012 07:03 AM   [ # 5 ]   [ Rating: 0 ]
Joined: 2010-01-20
34 posts
Muhammad Hasan Same - 26 April 2012 05:48 PM

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

I’ve just had a look at the Instagram Authentication Endpoints and they still don’t have an official Logout Endpoint.

In September 2011 a post was made to the Google Group to say they would add one soon.

http://groups.google.com/group/instagram-api-developers/msg/6e1f79587f087af5

Something like Rob suggested would clear the user credentials from your app.