Google trends Public Holidays Coupon Code Code Compiler

Login with Instagram using PHP


Jan 6, 2019

Here is the most important task for Login with Instagram feature implement in your web application using Instagram API. In this tutorial I will discuss its implementation. Allowing users to Login with Instagram is a good option for web applications. Instagram APIs through you can get user information such as username, ID, name, profile picture and a few more. However you cannot get email of the user.

Getting Started
First you have to register your application at instagr.am/developer

Now You can register as a client. So, Instagram will provide you client id and secret details.

Here is your Client ID and Secret Key

Now setup your valid redirect URL by clicking on security tab

Now implement In PHP: config.php


/* Instagram App Client Id */
define('INSTAGRAM_CLIENT_ID', 'INSTAGRAM_CLIENT_ID-XXXXXXX');

/* Instagram App Client Secret */
define('INSTAGRAM_CLIENT_SECRET', 'INSTAGRAM_CLIENT_SECRET-XXXXXXXXX');

/* Instagram App Redirect Url */
define('INSTAGRAM_REDIRECT_URI', 'INSTAGRAM_REDIRECT_URI-XXXXXXXX');

Create login with Instagram button: index.php

First Include here config.php


// include here config.php
require_once('config.php');

// Create login URl
$instURL = "https://api.instagram.com/oauth/authorize/?client_id=" . INSTAGRAM_CLIENT_ID . "&redirect_uri=" . urlencode(INSTAGRAM_REDIRECT_URI) . "&response_type=code&scope=basic";

Instagram button code


<html>
<head>
  <title>Login with Instagram</title>
</head>

<body>
	<a href="<?php echo $instURL; ?>">Login with Instagram</a>
</body>

</html>

function.php
Redirection page after login authentication success Instagram API Class will send the user details object in a array data format.


// Create API class for Instagram API
class InstagramAuth 
{
	public function GetToken($client_id, $redirect_uri, $client_secret, $code) {		
		$url = 'https://api.instagram.com/oauth/access_token';
		
		$urlPost = 'client_id='. $client_id . '&redirect_uri=' . $redirect_uri . '&client_secret=' . $client_secret . '&code='. $code . '&grant_type=authorization_code';
		$ch = curl_init();		
		curl_setopt($ch, CURLOPT_URL, $url);		
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_POST, 1);		
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $urlPost);			
		$data = json_decode(curl_exec($ch), true);	
		$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);	
		curl_close($ch); 		
		if($http_code != '200')			
			throw new Exception('Error : Failed to receive access token');
		
		return $data['access_token'];	
	}

	public function GetUserProfileInformation($access_token) { 
		$url = 'https://api.instagram.com/v1/users/self/?access_token=' . $access_token;	

		$ch = curl_init();		
		curl_setopt($ch, CURLOPT_URL, $url);		
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);	
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
		$data = json_decode(curl_exec($ch), true);
		$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);	
		curl_close($ch); 
		if($data['meta']['code'] != 200 || $http_code != 200)
			throw new Exception('Error : Failed to get user information');

		return $data['data'];
	}
}

success.php
Redirection page after login authentication success Instagram API will send the user details object in a array data format.

nbsp;


session_start();
// include here config.php
require_once('config.php');

// include here function.php
require_once('function.php');

// Instagram passes a parameter 'code' in the Redirect Url
if (isset($_GET['code'])) {
    try {
        $instagram_C = new InstagramAuth();

        // Get the access token 
        $access_token = $instagram_C->GetToken(INSTAGRAM_CLIENT_ID, INSTAGRAM_REDIRECT_URI, INSTAGRAM_CLIENT_SECRET, $_GET['code']);

        // Get user information
        $user_info = $instagram_C->GetUserProfileInformation($access_token);
        echo json_encode($user_info);
    } catch (Exception $e) {
        echo $e->getMessage();
        exit;
    }
}

Copyright 2024. All rights are reserved