Login with Instagram OAuth using PHP.

Sample database userstable 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) );
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.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 )); ?>
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>";
?>
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'];
}
}
?>
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');
}
?>
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}\">";
}
?>
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());
?>
Next post I will explain how to use instagram access token to access friend relations. If any queries please comment here.

















