Instagram PHP API

An easy-to-use PHP Class for accessing Instagram's API.

View the Project on GitHub cosenary/Instagram-PHP-API

Image Instagram PHP API V2

About

A PHP wrapper for the Instagram API. Feedback or bug reports are appreciated.

Composer package available.
Supports Instagram Video and Signed Header.

Requirements

Get started

To use the Instagram API you have to register yourself as a developer at the Instagram Developer Platform and create an application. Take a look at the uri guidlines before registering a redirect URI. You will receive your client_id and client_secret.


Please note that Instagram mainly refers to »Clients« instead of »Apps«. So »Client ID« and »Client Secret« are the same as »App Key« and »App Secret«.


A good place to get started is the example project.

Installation

I strongly advice using Composer to keep updates as smooth as possible.

Initialize the class

<?php
    require_once 'Instagram.php';
    use MetzWeb\Instagram\Instagram;

    $instagram = new Instagram(array(
      'apiKey'      => 'YOUR_APP_KEY',
      'apiSecret'   => 'YOUR_APP_SECRET',
      'apiCallback' => 'YOUR_APP_CALLBACK'
    ));

    echo "<a href='{$instagram->getLoginUrl()}'>Login with Instagram</a>";
?>

Authenticate user (OAuth2)

<?php
    // grab OAuth callback code
    $code = $_GET['code'];
    $data = $instagram->getOAuthToken($code);

    echo 'Your username is: ' . $data->user->username;
?>

Get user likes

<?php
    // set user access token
    $instagram->setAccessToken($data);

    // get all user likes
    $likes = $instagram->getUserLikes();

    // take a look at the API response
    echo '<pre>';
    print_r($likes);
    echo '<pre>';
?>

All methods return the API data json_decode() - so you can directly access the data.

Available methods

Setup Instagram

new Instagram(<array>/<string>);

array if you want to authenticate a user and access its data:

new Instagram(array(
  'apiKey'      => 'YOUR_APP_KEY',
  'apiSecret'   => 'YOUR_APP_SECRET',
  'apiCallback' => 'YOUR_APP_CALLBACK'
));

string if you only want to access public data:

new Instagram('YOUR_APP_KEY');

Get login URL

getLoginUrl(<array>)

getLoginUrl(array(
  'basic',
  'likes'
));

Optional scope parameters:

Scope Legend Methods
basic to use all user related methods [default] getUser(), getUserFeed(), getUserFollower() etc.
relationships to follow and unfollow users modifyRelationship()
likes to like and unlike items getMediaLikes(), likeMedia(), deleteLikedMedia()
comments to create or delete comments getMediaComments(), addMediaComment(), deleteMediaComment()

Get OAuth token

getOAuthToken($code, <true>/<false>)

true : Returns only the OAuth token
false [default] : Returns OAuth token and profile data of the authenticated user

Set / Get access token

User methods

Public methods

Authenticated methods

Sample responses of the User Endpoints.

Relationship methods

Authenticated methods

<?php
    // Follow the user with the ID 1574083
    $instagram->modifyRelationship('follow', 1574083);
?>

Please note that the modifyRelationship() method requires the relationships scope.


Sample responses of the Relationship Endpoints.

Media methods

Public methods

Sample responses of the Media Endpoints.

Comment methods

Public methods

Authenticated methods


Please note that the authenticated methods require the comments scope.


Sample responses of the Comment Endpoints.

Tag methods

Public methods

Sample responses of the Tag Endpoints.

Likes methods

Authenticated methods

How to like a Media: Example usage Sample responses of the Likes Endpoints.

All <...> parameters are optional. If the limit is undefined, all available results will be returned.

Instagram videos

Instagram entries are marked with a type attribute (image or video), that allows you to identify videos.

An example of how to embed Instagram videos by using Video.js, can be found in the /example folder.


Please note: Instagram currently doesn't allow to filter videos.


Signed Header

In order to prevent that your access tokens gets stolen, Instagram recommends to sign your requests with a hash of your API secret and IP address.

  1. Activate "Enforce Signed Header" in your Instagram client settings.
  2. Enable the signed-header in your Instagram class:

    $instagram->setSignedHeader(true);
  3. You are good to go! Now, all your POST and DELETE requests will be secured with a signed header.

Go into more detail about how it works in the Instagram API Docs.

Pagination

Each endpoint has a maximum range of results, so increasing the limit parameter above the limit won't help (e.g. getUserMedia() has a limit of 90).

That's the point where the "pagination" feature comes into play. Simply pass an object into the pagination() method and receive your next dataset:

<?php
    $photos = $instagram->getTagMedia('kitten');

    $result = $instagram->pagination($photos);
?>

Iteration with do-while loop.

Samples for redirect URLs

Registered Redirect URI Redirect URI sent to /authorize Valid?
http://yourcallback.com/ http://yourcallback.com/ yes
http://yourcallback.com/ http://yourcallback.com/?this=that yes
http://yourcallback.com/?this=that http://yourcallback.com/ no
http://yourcallback.com/?this=that http://yourcallback.com/?this=that&another=true yes
http://yourcallback.com/?this=that http://yourcallback.com/?another=true&this=that no
http://yourcallback.com/callback http://yourcallback.com/ no
http://yourcallback.com/callback http://yourcallback.com/callback/?type=mobile yes

If you need further information about an endpoint, take a look at the Instagram API docs.

Example App

Image

This example project, located in the example/ folder, helps you to get started. The code is well documented and takes you through all required steps of the OAuth2 process. Credit for the awesome Instagram icons goes to Ricardo de Zoete Pro.

More examples and tutorials:

Let me know if you have to share a code example, too.

History

Version 3.0 is in development and includes support for real-time subscriptions.

Instagram 2.2 - 04/10/2014

Instagram 2.1 - 30/01/2014

Instagram 2.0 - 24/12/2013

Instagram 2.0 beta - 20/11/2013

Instagram 2.0 alpha 4 - 01/11/2013

Instagram 2.0 alpha 3 - 04/09/2013

Instagram 2.0 alpha 2 - 14/06/2013

Instagram 2.0 alpha 1 - 28/05/2012

Instagram 1.5 - 31/01/2012

Instagram 1.0 - 20/11/2011

Instagram 0.8 - 16/11/2011

Instagram 0.5 - 12/11/2011

Credits

Copyright (c) 2011-2014 - Programmed by Christian Metz

Released under the BSD License.

Bitdeli Badge