Instagram – Get Access Token and User Profile

  1. The first – Create your Client for Instagram
Create client
Client details

2. Next step – add redirect url for Client

3. Create API on your localhost
Create 2 files: index.php and response.php


File index.php

$clientId = 'YOUR_CLIENT_ID';
$redirectUri = 'http://localhost/instagram/response.php';
$url = "https://api.instagram.com/oauth/authorize/?client_id={$clientId}&redirect_uri={$redirectUri}&response_type=code";
header("Location:{$url}");


File response.php

$clientId = 'YOUR_CLIENT_ID';
$clientSecret = 'YOUR_CLIENT_SECRET';
$redirectUri = 'http://localhost/instagram/response.php';
$code = $_GET['code'];
$url = "https://api.instagram.com/oauth/access_token";
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,
    "client_id={$clientId}&client_secret={$clientSecret}&grant_type=authorization_code&redirect_uri={$redirectUri}&code={$code}");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$server_output = curl_exec($ch);
$server_output = json_decode($server_output);
curl_close ($ch);
print_r($server_output);

The last: go to your browser and type: http://localhost/instagram

About the Author: admin

Leave a Reply

Your email address will not be published. Required fields are marked *