Friday, June 16, 2017

Instagram: How to get images using Instagram API

You can develop a web-app to show Instagram images/photos from other users accounts using API call. Instagram API is the easiest so far to me. At first you have to visit https://www.instagram.com/developer/ to create a developer account and then an application for API communication as below steps:

The most important link is https://www.instagram.com/developer which describes API procedures.







You can create a Application and then you have client id and client secret as below screenshot.



Below is the full PHP code snippet to get images/photos from Instagram using API call:


<?php
session_start();

include_once "CurlExecutor.php";

define("CLIENT_ID", "d1aa0f7.............................");
define("CLIENT_SECRET", "e972a4a.........................");
define("REDIRECT_URI", "http://localhost/ci/InstagramAPI/");

if (isset($_SESSION["access_token"])) {
    getRecentPhotos();
}
elseif (isset($_GET["code"])) {
    getAccessToken();
}
else {
    authentication();
}

function getRecentPhotos() {
    $user_id = $_SESSION["user_id"];
    $url = "https://api.instagram.com/v1/users/$user_id/media/recent/";
    $url = $url . "?access_token=" . $_SESSION["access_token"];
    $url = $url . "&count=5";
    $response = CurlExecutor::execute($url);
    $response["response"] = json_decode($response["response"]);
    if ($response["code"] != 200) {
        die("ERROR");
    }
    foreach ($response["response"]->data as $o) {
        echo "<a target='_blank' href='" . $o->link . "'>";
        echo "<img src='" . $o->images->low_resolution->url . "'/>";
        echo "</a>";
    }
}

function getAccessToken() {
    $post = array(
        "client_id" => CLIENT_ID,
        "client_secret" => CLIENT_SECRET,
        "grant_type" => "authorization_code",
        "redirect_uri" => REDIRECT_URI,
        "code" => $_GET["code"]
    );
    $response = CurlExecutor::execute("https://api.instagram.com/oauth/access_token", "POST", $post);
    $response["response"] = json_decode($response["response"]);
    CurlExecutor::prettyPrint($response);
    if ($response["code"] != 200) {
        die("ERROR");
    }
    $_SESSION["access_token"] = $response["response"]->access_token;
    $_SESSION["user_id"] = $response["response"]->user->id;
    header("Refresh:0; url=" . REDIRECT_URI);
}

function authentication() {
    $auth_url = "https://api.instagram.com/oauth/authorize/?" .
        "client_id=" . CLIENT_ID .
        "&redirect_uri=" . REDIRECT_URI .
        "&response_type=code";
    header("Refresh:0; url=$auth_url");
    exit();
}
?>

Authorization screen is as below screenshots (User will authorize this application and then forwarded to redirect URI with a one time code, you can use that code to get access token, and then you can get users photos as well as users basic info using those access token):



And output is as below:






1 comment:

  1. Thank you for the informative guide on accessing images through the Instagram API! Your detailed instructions provided valuable insights into leveraging this powerful tool effectively. With your help, navigating the complexities of the API seems much more manageable. I appreciate the clarity and thoroughness of your explanation, which has undoubtedly facilitated my understanding of this process. Your contribution is immensely helpful in my journey towards utilizing Instagram's features to enhance my projects.

    ReplyDelete