The very quick registration gives you many users to your web project, we already published login with Facebook, Twitter and Google Plus now time to think about very popular photo sharing portal Instagram. This post explains you how to login with instagram Oauth API and importing user data. Create an instagram account and take a quick look at this demo thanks!

Download Script
Live DemoDatabase Table
Sample database users table columns id, username, name, bio, website, instragram_id and instagram_access_token.
CREATE TABLE users
(
id INT PRIMARY KEY AUTO_INCREMENT,
username VARCHAR(70),
name VARCHAR(100),
bio TEXT,
website VARCHAR(200),
instagram_id INT,
instagram_access_token VARCHAR(200)
);
(
id INT PRIMARY KEY AUTO_INCREMENT,
username VARCHAR(70),
name VARCHAR(100),
bio TEXT,
website VARCHAR(200),
instagram_id INT,
instagram_access_token VARCHAR(200)
);
Getting Started
First you have to register your application at instagr.am/developer

Instagram Register new OAuth Client
Give your domain details and redirection callback page URL

The tutorial contains few PHP files with Instagram Oauth class file.
instagram.class.php // Instagram Class
instagram.config.php // Instagram Key Configuration
index.php
home.php
success.php // Redirection page
popular_media.php // Instagram popular images
db.php // Database Config
instagram.config.php // Instagram Key Configuration
index.php
home.php
success.php // Redirection page
popular_media.php // Instagram popular images
db.php // Database Config
Application Key Details
Instagram will provide you client id and secret details.

instagram.config.php
Here you have to give your application key details.
<?php
$instagram = new Instagram(array(
'apiKey' => 'Client_ID',
'apiSecret' => 'Client_Secret',
'apiCallback' => 'http://www.yourdomain.com/success.php' // Callback URL
));
?>
$instagram = new Instagram(array(
'apiKey' => 'Client_ID',
'apiSecret' => 'Client_Secret',
'apiCallback' => 'http://www.yourdomain.com/success.php' // Callback URL
));
?>
index.php
Instagram login link page.
<?php
session_start();
// User session data availability check
if (!empty($_SESSION['userdetails']))
{
// Redirecting to home.php
header('Location: home.php');
}
require 'instagram.class.php';
require 'instagram.config.php';
// Login URL
$loginUrl = $instagram->getLoginUrl();
echo "<a href='$loginUrl'>Sign in with Instagram </a>";
?>
session_start();
// User session data availability check
if (!empty($_SESSION['userdetails']))
{
// Redirecting to home.php
header('Location: home.php');
}
require 'instagram.class.php';
require 'instagram.config.php';
// Login URL
$loginUrl = $instagram->getLoginUrl();
echo "<a href='$loginUrl'>Sign in with Instagram </a>";
?>
success.php
Redirection page after login authentication success instagram API will send the user details object in a array data format. Here the system will insert data into USERS table, read the comments.
<?php
require 'db.php';
require 'instagram.class.php';
require 'instagram.config.php';
// Receive OAuth code parameter
$code = $_GET['code'];
// Check whether the user has granted access
if (true === isset($code))
{
// Receive OAuth token object
$data = $instagram->getOAuthToken($code);
if(empty($data->user->username))
{
header('Location: index.php');
}
else
{
session_start();
// Storing instagram user data into session
$_SESSION['userdetails']=$data;
$user=$data->user->username;
$fullname=$data->user->full_name;
$bio=$data->user->bio;
$website=$data->user->website;
$id=$data->user->id;
$token=$data->access_token;
// Verify user details in USERS table
$id=mysql_query("select instagram_id from instagram_users where instagram_id='$id'");
if(mysql_num_rows($id) == 0)
{
// Inserting values into USERS table
mysql_query("insert into instagram_users(username,Name,Bio,Website,instagram_id,instagram_access_token) values('$user','$fullname','$bio','$website','$id','$token')");
}
// Redirecting you home.php
header('Location: home.php');
}
}
else
{
// Check whether an error occurred
if (true === isset($_GET['error']))
{
echo 'An error occurred: '.$_GET['error_description'];
}
}
?>
require 'db.php';
require 'instagram.class.php';
require 'instagram.config.php';
// Receive OAuth code parameter
$code = $_GET['code'];
// Check whether the user has granted access
if (true === isset($code))
{
// Receive OAuth token object
$data = $instagram->getOAuthToken($code);
if(empty($data->user->username))
{
header('Location: index.php');
}
else
{
session_start();
// Storing instagram user data into session
$_SESSION['userdetails']=$data;
$user=$data->user->username;
$fullname=$data->user->full_name;
$bio=$data->user->bio;
$website=$data->user->website;
$id=$data->user->id;
$token=$data->access_token;
// Verify user details in USERS table
$id=mysql_query("select instagram_id from instagram_users where instagram_id='$id'");
if(mysql_num_rows($id) == 0)
{
// Inserting values into USERS table
mysql_query("insert into instagram_users(username,Name,Bio,Website,instagram_id,instagram_access_token) values('$user','$fullname','$bio','$website','$id','$token')");
}
// Redirecting you home.php
header('Location: home.php');
}
}
else
{
// Check whether an error occurred
if (true === isset($_GET['error']))
{
echo 'An error occurred: '.$_GET['error_description'];
}
}
?>
home.php
Welcome page here you can display user data accessing session userdetails value.
<?php
session_start();
if($_GET['id']=='logout')
{
unset($_SESSION['userdetails']);
session_destroy();
}
require 'instagram.class.php';
require 'instagram.config.php';
if (!empty($_SESSION['userdetails']))
{
$data=$_SESSION['userdetails'];
echo '<img src='.$data->user->profile_picture.' >';
echo 'Name:'.$data->user->full_name;
echo 'Username:'.$data->user->username;
echo 'User ID:'.$data->user->id;
echo 'Bio:'.$data->user->bio;
echo 'Website:'.$data->user->website;
echo 'Profile Pic:'.$data->user->profile_picture;
echo 'Access Token: '.$data->access_token;
// Store user access token
$instagram->setAccessToken($data);
// Your uploaded images
$popular = $instagram->getUserMedia($data->user->id);
foreach ($popular->data as $data) {
echo '<img src='.$data->images->thumbnail->url.'>';
}
// Instagram Data Array
print_r($data);
}
else
{
header('Location: index.php');
}
?>
session_start();
if($_GET['id']=='logout')
{
unset($_SESSION['userdetails']);
session_destroy();
}
require 'instagram.class.php';
require 'instagram.config.php';
if (!empty($_SESSION['userdetails']))
{
$data=$_SESSION['userdetails'];
echo '<img src='.$data->user->profile_picture.' >';
echo 'Name:'.$data->user->full_name;
echo 'Username:'.$data->user->username;
echo 'User ID:'.$data->user->id;
echo 'Bio:'.$data->user->bio;
echo 'Website:'.$data->user->website;
echo 'Profile Pic:'.$data->user->profile_picture;
echo 'Access Token: '.$data->access_token;
// Store user access token
$instagram->setAccessToken($data);
// Your uploaded images
$popular = $instagram->getUserMedia($data->user->id);
foreach ($popular->data as $data) {
echo '<img src='.$data->images->thumbnail->url.'>';
}
// Instagram Data Array
print_r($data);
}
else
{
header('Location: index.php');
}
?>
popular_media.php
Get Instagram popular media.
<?php
require 'instagram.class.php';
// Initialize class for public requests
$instagram = new Instagram('Client_ID');
// Get popular instagram media
$popular = $instagram->getPopularMedia();
// Display results
foreach ($popular->data as $data)
{
echo "<img src="\"{$data->images->thumbnail->url}\">";
}
?>
require 'instagram.class.php';
// Initialize class for public requests
$instagram = new Instagram('Client_ID');
// Get popular instagram media
$popular = $instagram->getPopularMedia();
// Display results
foreach ($popular->data as $data)
{
echo "<img src="\"{$data->images->thumbnail->url}\">";
}
?>
db.php
Database configuration file you have to modify username, password and database name values.
<?php
define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'Username');
define('DB_PASSWORD', 'Password');
define('DB_DATABASE', 'Database');
$connection = mysql_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD) or die(mysql_error());
$database = mysql_select_db(DB_DATABASE) or die(mysql_error());
?>
define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'Username');
define('DB_PASSWORD', 'Password');
define('DB_DATABASE', 'Database');
$connection = mysql_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD) or die(mysql_error());
$database = mysql_select_db(DB_DATABASE) or die(mysql_error());
?>
Next post I will explain how to use instagram access token to access friend relations. If any queries please comment here.









Great article, thanks!
a nice one!!!
this is wonderful!!!
Nice work buddy! but i think you should think about yahoo oauth sys.
asslm..mau coba dl mas bro..thanks ya
nice!
Nice
ey...
bookmark!
Super! Super Great Article. Nice! Thanks!!!
Good Tutorial ,
thanks a lot
Hi,
I just saw that you used my Instagram class in your tutorial. It's great that you found it helpful, because as a developer your are always glad to see that your work is apprecited. Thanks too, for your other tutorials wich are very useful for me. Keep up with your really good work.
All the best,
Christian
in the script success.php
$ id = $ data-> user-> id;
Writes a value into the database - "Resource id # 7"
Why is it so?
Nice one. And let's hope my full_name is not "'); DROP instagram_users; ( "
I changed
$popular = $instagram->getPopularMedia();
to
$popular = $instagram->getUserLikes();
and
$popular = $instagram->getUserLikes('MY_ID');
but not shown anything.
How to???? whats wrong?
Heya, I'm having a real hard time with authentication. I need to dynamically call the access token and I can't seem to get it right. I have:
$api = 'https://api.instagram.com/v1/users/self/media/recent?access_token=[CODE];.
I tried:
$api = 'https://api.instagram.com/v1/users/self/media/recent?access_token=.'$token;
and that didn't work.
I even tried:
$api = 'https://api.instagram.com/v1/users/self/media/recent?access_token='.$data->access_token;
and that also didn't work. Any suggestions? Thanks!
Hello .. I'm getting this error:
Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /instagram/index.php:7) in /instagram/index.php on line 56
On the index.php ... the a load of errors after it's authenticated.
Any ideas??
Thanks
Great tutorial, I'm hoping you show how to grab posts using certain user keys. That would be useful.
wow... Great tutorial, thank you very much!!!
great tutorial..
nice tutorial i liked it.
thanks for this.
Great tutorial, The demo doesn't have an error condition for checking if user has denied the request - would be a complete demo with that :)
Awesome, is it possible to use with scopes?
scope=likes+comments
I'm having some issues and would love if any of you could help me!
I've managed to get up to the step of when the user allowes the auth. , and then the url is redirected to the 'success.php' page, which should then redirect them to 'home.php'? however this doesn't happen, i am just presented with a webpage displaying the source code of 'success.php',
it would be great if anyone could let me know where I've gone wrong, or what can i do to fix this!?
Please email me at josh.calvert@instapouch.com
Thanks!
NICE ONE DUDE! It helps me a lot integrating API's..:D
How can I write to a txt or xml file instead of inserting into a DB? I'm not provided a DB with my service, but would like to show my instagram photos on my site.
Hi, where is the instagram.class.php??
Thanks for showing step by step process of using instagram in PHP. It's really useful and I should say every PHP programmer must know about this.
Very nice tutorial, and realy very helpfull!
thanks a lot!
But I have a question!
How to show, all user's photos on the "home.php" page?
I know that should be used pagination... but I don't understand, how to do it...
Can you help me?
Great article, but why would you store the user bio and website, when these things can change. Won't you have stale data on your db?
This helped me alot! Thanks! :)
Great, but how to log out of it?
save my time, thanks
Hello thanks for this tutorial !! I want to give the possibility to the user to select one or many picture (in the high Res) from his instagram pictures and send the selectionned picture attach to a php form. has someone an id to help me to make that ? Matthieu
what is instagram.class.php?
Awesome tut!!!
But i've question! how i can show images with different size and pagination?
{"code": 400, "error_type": "OAuthException", "error_message": "Redirect URI does not match registered redirect URI"}
I did everything but get this error when i want to auth my app
when i am trying to login it redirects to the same index page with login button. Please help me to solve this issue
thats what I was searching. thanks
Nice!
You should change the database name on this page to "instagram_users" instead of "users", otherwise it doesn't work. Thanks for the tut! Regards.