Yii and instagram API

Yii and instagram API PHP

Instagram is a free photo-sharing program and social network that was launched in October 2010. The service enables users to take a photo, apply a digital filter to it, and then share it with other Instagram users they are connected to on the social network as well as on a variety of social networking services.

Too many developed great apps for this platform such as statigr.am which is a great implementation of the public API.

instagram gives developers a great API, however there’s a php API of course based on the original instagram API but much more simpler to use in your PHP project.

This API called PHP-Instagram-API .

To use this API in your Yii project please download it from here.

extract to downloaded ZIP file. You will see three folders “Examples”, “Instagram”, “Tests”.
Just take “Instagram” folder and place it under “protected/components/”.

I created a nice class and called it InstaConnect, which you can place all your functions inside it (in components folder).

instagram = new Instagram\Instagram;
       $this->instagram->setClientID('');// client ID of you app on instagram
       $this->instagram->setAccessToken(''); //don't forget to get an valid access token
    }

    //return all #love tag media as an array
    public function getLoveMedia() {
      $tag = $this->instagram->getTag('love');
      $media = $tag->getMedia( isset( $_GET['max_tag_id'] ) ? array( 'max_tag_id' => $_GET['max_tag_id'] ) : null );
      
      return $media;
    }
    
}

So to implement this class please see the “Example” folder and add the functions you need.
As you can see before you can use the API you should obtain the ClientID and AccessToken.

ClientID
Go to instagram website and register your website and your app.

Authentication (AccessToken)
Set up a client for use with Instagram’s API

Create an Auth Object and pass in the information from the API

$auth_config = array(
    'client_id'         => '',
    'client_secret'     => '',
    'redirect_uri'      => '',
    'scope'             => array( 'likes', 'comments', 'relationships' )
);

$auth = new Instagram\Auth( $auth_config );

Then you have to get the user to authorize your app

$auth->authorize();

This will redirect the user to the Instagram authorization page. After authorization Instagram will redirect the user to the url in $auth_config[‘redirect_uri’] with a code that you will need to obtain an access token

$accessToken = $auth->getAccessToken( $_GET['code'] );

Then use the access token in your code.

Limits:
Be nice. If you’re sending too many requests too quickly, you will have a 503 error code (server unavailable).

You are limited to 5000 requests per hour per access_token or client_id overall. Practically, this means you should (when possible) authenticate users so that limits are well outside the reach of a given user.

Note:

  • There’s a Yii extension, but I prefer to use a simple API instead.
  • PHP instagram API works only with PHP 5.3+ since it’s using namespacing.

if you have any questions please feel free to comment.
Thanks for reading.

If you think something is wrong please feel free to comment :)

Leave a reply.