A PHP wrapper for the Instagram API.
This is my first PHP class, so please bear with me.
Feedback or bug reports are appreciated.
- PHP 5.2.x or higher
- cURL
- Registered Instagram App
Register your application with Instagram, and receive your OAuth client_id
and client_secret
.
Take a look at the uri guidlines before registering a Redirect URI.
A good place to get started is the example App.
<?php
require_once 'instagram.class.php';
$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>";
?>
<?php
// Grab OAuth callback code
$code = $_GET['code'];
$data = $instagram->getOAuthToken($code);
echo 'Your username is: ' . $data->user->username;
?>
<?php
// Store 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.
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');
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 | coming soon... |
getOAuthToken($code, <true>/<false>)
true
: Returns only the OAuth token
false
[default] : Returns OAuth token and profile data of the authenticated user
Stores access token, for further method calls:
setAccessToken($token)
Returns access token, if you want to store it for later usage:
getAccessToken()
Public methods
getUser($id)
searchUser($name, <$limit>)
Authenticated user methods
getUser()
getUserLikes(<$limit>)
getUserFeed(<$limit>)
getUserMedia(<$id>, <$limit>)
- if an
$id
isn't defined, it returns the media of the logged in user
- if an
Authenticated user methods
getUserFollows(<$id>, <$limit>)
getUserFollower(<$id>, <$limit>)
getUserRelationship($id)
modifyRelationship($action, $user)
$action
: Action command (follow / unfollow / block / unblock / approve / deny)$user
: Target user id
<?php
// Follow the user with the ID 1574083
$instagram->modifyRelationship('follow', 1574083);
?>
Public methods
getMedia($id)
getPopularMedia()
searchMedia($lat, $lng, <$distance>)
$lat
and$lng
are coordinates and have to be floats like:48.145441892290336
,11.568603515625
$distance
Distance in meter (max. distance: 5km = 5000) (radial?)
All <$limit>
parameters are optional. If the limit is undefined, all available results will be returned.
Public methods
getTag($name)
getTagMedia($name)
searchTags($name)
Authenticated user methods
getMediaLikes($id)
likeMedia($id)
deleteLikedMedia($id)
How to like a Media: Example usage Sample responses of the Likes Endpoints.
It's planned to extend the class with new methods. Let me know, if you think, that one of the missing endpoints has priority.
Missing Endpoints:
Comments
, Locations
, Geographies
For all parameters in the configuration array exists a public setter and getter method.
This feature is still in development, but you can test it on the dev branch: Pagination documentation.
If you need additional informations, take a look at Instagrams API docs.
The small App, which is located in the example/
folder, helps you to get started with the class.
Its whole code is documented and will take you through all steps of the OAuth2 process.
The great Instagram Sign In button is designed by Murat Mutlu.
A short tutorial about how to build an Instagram login with my class has been published at 9lessons.
Instagram 1.7 - 07/08/2012
feature
Added Likes endpointschange
Addeddistance
parameter tosearchMedia()
method (thanks @jonathanwkelly)
Instagram 1.6 - 22/05/2012
feature
Added User Relationship endpointsfeature
Added scope parameter table for thegetLoginUrl()
method
Instagram 1.5 - 31/01/2012
release
Second master versionfeature
Added Tag endpointschange
Edited the "Get started" examplechange
Now you can pass thegetOAuthToken()
object directly intosetAccessToken()
Instagram 1.0 - 20/11/2011
release
First public releasefeature
Added sample App with documented codeupdate
New detailed documentation
Instagram 0.8 - 16/11/2011
release
First inital released versionfeature
Initialize the class with a config array or string (see example)
Instagram 0.5 - 12/11/2011
release
Beta versionupdate
Small documentation
Copyright (c) 2011-2012 - Programmed by Christian Metz
Released under the BSD License.