How to login with Instagram using PHP
Today, we will learn How to login with Instagram using PHP. Most-of case we use google login, facebook login and twitter login. But today we will implement Instagram login in PHP. We’ll use the Instagram OAuth API, which is an easy and powerful way to add Instagram login to your site.
Create login with Instagram button
config.php
<?php
define('CLIENT_ID', 'CLIENT_ID-XXXXXXX'); //Client Id
define('CLIENT_SECRET', 'CLIENT_SECRET-XXXXXXXXX'); //Client Secret
define('REDIRECT_URI', 'REDIRECT_URI-XXXXXXXX'); //Redirect Url
index.php
<?php
require_once('config.php');
$instagramUrl = "https://api.instagram.com/oauth/authorize/?client_id=".CLIENT_ID . "&redirect_uri=" . urlencode(REDIRECT_URI) . "&response_type=code&scope=basic"; //Create login URl
?>
<html>
<head>
<title>How to login with Instagram using PHP</title>
</head>
<body>
<h1>How to login with Instagram using PHP</h1>
<a href="<?php echo $instagramUrl; ?>" class="custom-button">Login with Instagram</a>
</body>
</html>
function.php
Login authentication success, Instagram API Class will send the user details object data in the array format.
<?php
class InstaAuth {
public function GetAccessToken($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 : Something went wrong. Message: Failed to receive access token');
}
return $data['access_token'];
}
public function GetProfileInformation($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 : Something went wrong. Message: Failed to get user information');
}
return $data['data'];
}
}
success.php
<?php
session_start();
require_once('config.php');
require_once('function.php');
if (isset($_GET['code'])) {
try {
$insta_auth = new InstaAuth();
$access_token = $insta_auth->GetToken(CLIENT_ID, REDIRECT_URI, CLIENT_SECRET, $_GET['code']); //Get the access token
// Get the user information
$user_information = $insta_auth->GetProfileInformation($access_token);
echo json_encode($user_information);
} catch (Exception $e) {
echo $e->getMessage();
exit;
}
} else {
header("location: index.php");
}