This plugin has the following requirements:
- CakePHP 3.0.0 or greater.
- PHP 5.4.16 or greater.
You can install this plugin into your CakePHP application using composer.
composer require multidots/cakephp-instagram
After installation, Load the plugin
Plugin::load('Instagram', ['bootstrap' => true]);
Or, you can load the plugin using the shell command
$ bin/cake plugin load -b Instagram
This plugin provides a Utility class to connect with Instagram APIs. Simply create an instance of this class and perform API calls.
Instagram API uses the OAuth 2.0 protocol and requires access_token
for all authenticated requests.
Before making any API call, you need to create a new instance of InstagramClient class in your application and pass your client id and client secret.
First of all, write a configuration variable with client id, secret and redirect url like,
$instaConfig = [
'clientId' => 'your-client-id',
'clientSecret' => 'your-client-secret',
'redirectUri' => 'your-redirect-url'
];
Configure::write('Instagram.config', $instaConfig);
After this, create an instance and pass the configuration values like,
$instagramClient = new InstagramClient(Configure::read('Instagram.config'));
To make an authenticated call, Instagram API requires access_token in request. To get an access token, you need to redirect user to Instagram authentication URL.
This plugin provides a Helper to generate authentication URL for your user. Load the Instagram helper in your application and use it's getAuthUrl()
method to generate URL.
In your AppView,
$this->loadHelper('Instagram.Instagram');
In your template,
<a href="<?= $this->Instagram->getAuthUrl(); ?>">Authorize with Instagram</a>
After successful authentication by user, Instagram will redirect to your specified redirect uri with code
.
In your action, make an API call to get an access_token
from the code like,
$response = $instagramClient->getAccessToken($this->request->query('code'));
$accessToken = $response->access_token; // You may want to store it into database for future use
Once you have the access_token
from Instagram, set it via accessToken()
method.
$instagramClient = new InstagramClient(Configure::read('Instagram.config'));
$instagramClient->accessToken('your-access-token');
Now it is ready to make authenticated API calls.
As of now, this plugin provides following API methods.
Get profile information about the self/specific user. It supports following parameters:
- user_id: User id (self will be used if not set)
$instagramClient = new InstagramClient($options);
$instagramClient->accessToken($accessToken);
// own profile
$response = $instagramClient->getProfile();
// specific user profile
$params = ['user_id' => 1];
$response = $instagramClient->getProfile($params);
Get the most recent media published by a user. It supports following parameters:
- user_id: User id (self will be used if not set)
- url: API url (mostly used in pagination (next_url), if set other
- options will be ignored)
- count: Count of media to return
- min_id: Return media later than this min_id
- max_id: Return media earlier than this max_ids
$instagramClient = new InstagramClient($options);
$instagramClient->accessToken($accessToken);
$params = ['count' => 20];
$response = $instagramClient->getMedia($params);