Code Monkey home page Code Monkey logo

cake-instagram-datasource's Introduction

Instagram API

Provides a DataSource for connecting to the Instagram API. The data source allows you to get and modify entries as well as provides means to

Installation

To use the Instagram API you need to have a client setup. See the client manager to setup a new client or use an existing one. The API uses the client ID, client secret and redirect URL to authenticate with the Instagram API. Note that most of the general read actions do not require that an Instagram user is logged in, so there may never be a need to authenticate the client.

app/Config/bootstrap.php

<?php
CakePlugin::load('InstagramApi');
?>

app/Config/database.php

<?php
class DATABASE_CONFIG {

	public $sample = array(
		'datasource'    => 'InstagramApi.InstagramSource',
		'client_id'     => '' // from your Instagram client
		'client_secret' => '' // from your Instagram client
		'redirect_url'  => '' // from your Instagram client
	);

}
?>

Usage

The data source works as a wrapper around the API end points specified in the Instagram API documentation and is used by directly calling the CRUD methods in the data source with the specified endpoint and an array of parameters. However, the client ID (or access token if the connection has been autherized) is automatically added to the parameters sent to the API.

The response data is parsed from JSON, so a generic object is returned from the methods (or false in the case of an error).

Examples

<?php

// Search for a tag called "test"
$instagram = ConnectionManager::getDataSource('instagram');
$tags = $instagram->read('tags/search', array(
	'q' => 'test'
));

// Delete a comment
$instagram = ConnectionManager::getDataSource('instagram');
$result = $instagram->delete('media/1234/comment', 4321);

// Add a like (requires authentication)
$instagram = ConnectionManager::getDataSource('instagram');
$result = $instagram->create('media/1234/like');

?>

Authorization

Authorization with the Instagram API requires two steps, much like the Facebook API. You need to have set up the client_id, client_secret and redirect_url in the database configuration (see above) and they must match the ones in your Instagram client manager.

First, you call the InstagramSource::authenticate() method with no parameters to get the URL to send the user to.

<?php
$instagram = ConnectionManager::getDataSource('instagram');
$url = $instagram->authenticate();
$this->redirect($url);
?>

Once the user has authenticated your client, he will be sent back to the URL specified in redirect_url, but with the query parameter code attached. To authenticate the Instagram API, you need to send this code to the data source. This will contact Instagram and retrieve a response object including an access token and a user object.

<?php
$code = $this->request->query['code'];
$response = $instagram->authenticate($code);
$token = $response->access_token;
$user = $response->user;
?>

The data source doesn't store the token, so you'll need to save it (in the database or session or wherever) and when you want to authorize your client with Instagram you call the method with the token and true as the other parameter, which will authorize the API.

<?php
$instagram->authorize($token, true);
?>

cake-instagram-datasource's People

Watchers

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