sofwar/instagram

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

1.0.0 2016-09-24 14:48 UTC

This package is auto-updated.

Last update: 2024-11-28 23:40:00 UTC


README

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

Composer package available.

Requirements

  • PHP 5.4 or higher
  • cURL
  • Registered Instagram App

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 guidelines 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«.

Installation

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

$ composer require sofwar/instagram

Initialize the class

use SofWar\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)

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

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

Get user likes

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

Get OAuth token

getOAuthToken($code, <boolean>)

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

Set / Get access token

  • Set the access token, for further method calls: setAccessToken($token)
  • Get the access token, if you want to store it for later usage: getAccessToken()

User methods

  • getUser(<$id>)
  • searchUser($name, <$limit>)
  • getUserMedia($id, <$limit>)
  • getUserLikes(<$limit>, <$max_like_id>)
  • getUserMedia(<$id>, <$limit>)
    • if an $id isn't defined or equals 'self', it returns the media of the logged in user

Sample responses of the User Endpoints.

Relationship methods

  • getUserFollows(<$limit>)
  • getUserFollower(<$limit>)
  • getUserRelationship($id)
  • modifyRelationship($action, $user)
    • $action : Action command (follow / unfollow / approve / ignore)
    • $user : Target user id
// Follow the user with the ID 1521204717
$instagram->modifyRelationship('follow', 1521204717);

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

Sample responses of the Relationship Endpoints.

Media methods

  • getMedia($id)
  • getMediaShort($code)
  • searchMedia($lat, $lng, <$distance>)

Sample responses of the Media Endpoints.

Comment methods

  • getMediaComments($id)
  • addMediaComment($id, $text)
  • deleteMediaComment($id, $commentID)

Please note that the authenticated methods require the comments scope.

Sample responses of the Comment Endpoints.

Tag methods

  • getTag($name)
  • getTagMedia($name, <$limit>, <$min_tag_id>, <$max_tag_id>)
  • searchTags($name)

Sample responses of the Tag Endpoints.

Likes methods

Authenticated methods

  • getMediaLikes($id)
  • likeMedia($id)
  • deleteLikedMedia($id)

Sample responses of the Likes Endpoints.

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

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, the called endpoint and parameters.

  1. Activate "Enforce Signed Header" in your Instagram client settings.
  2. Enable the signed-header in your Instagram class:
$instagram->setSignedHeader(true);
  1. You are good to go! Now, all your 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:

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

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

Iteration with do-while loop.

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

Changelog

Please see the changelog file for more information.

Released under the BSD License.