Code Monkey home page Code Monkey logo

cakephp-instagram's Introduction

Instagram plugin for CakePHP 3

Build Status GitHub license Latest Stable Version Total Downloads

Requirements

This plugin has the following requirements:

  • CakePHP 3.0.0 or greater.
  • PHP 5.4.16 or greater.

Installation

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

Usage

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.

Basic setup

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'));

Authenticate User

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>

Handle access token from response

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

Set access token

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.

API Methods

As of now, this plugin provides following API methods.

Users

Profile

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);
Recent Media

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

cakephp-instagram's People

Contributors

narendravaghela avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.