Create a Instagram User Image Fetcher in PHP

Today I will show you how to create a simple Instagram user image fetcher in PHP by using Instagram REST API. This application will use  Instagram API to fetch recent 20 photos of public users with the similar name. It also displays likes of each photo. Please note that you can only access photos of users who make their profile public using this API.

If you are not familiar with REST then REST stands for Representational State Transfer which is a way of accessing web services. 75% of web services use REST. You can access a REST service by using a simple HTTP GET or POST requests. The API give output most probably in JSON  format and you can decode it in PHP and use it in your application.

I will make a tutorial on how to make your own REST API in PHP Soon. If you are eager to learn How to Create an Instagram Location Image Fetcher using Google map API and Instagram API you can view the detailed tutorial on that by visiting following link

Now lets starts

Get Latest Images by using Instagram API

        The Instagram API's requires an ACCESS TOKEN. Here is the REST API URL. The output is also in JSON Format.{user-id}/media/recent/?access_token=ACCESS-TOKEN

Note: First API is used to fetches users id's of the user with similar username and the second API is used to fetch recent 20 images of that user.

Now in order to get access token, there are again two methods

First Method

        Register your app in Instagram developer console and get access token by using again using GET  Request like below

         But this access token can be used to fetch your own information only not any other users. In order to fetch public users information you need to submit your app on Instagram which is a very complex process so the second method

Second Method (Recommended and Easy)

         Just visit the following link to get the Access Token.

Once you get the access token then you can easily fetch images of users by calling above two API's using PHP script as given below.

The Complete Code


$access_token = "518______63e";
$userid_url = ''.urlencode($_POST['username']).'&access_token='.$access_token;

$userid_json = file_get_contents($userid_url);
$userid_array = json_decode($userid_json,true);

<!DOCTYPE html>
<html lang="en">
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="">
  <script src=""></script>
  <script src=""></script>

<div class="jumbotron text-center">
  <h1>Instagram User Image Searcher</h1>
<div class="container">
	<div class="row">
		<div class="col-md-8 col-md-offset-2">
			<form action="" method="POST">
			  <div class="form-group">
				<label for="location">User Name</label>
				<input type="test" class="form-control" name="username" value="<?php if(!empty($_POST['username']))echo $_POST['username'];?>">
			  <button type="submit" class="btn btn-primary btn-block">SEARCH</button>
	<div class="row">
		{   ?>
				<div class="col-md-8 col-md-offset-2">
					foreach($userid_array['data'] as $id)
						$userid = $id['id'];
                        $username = $id['full_name'];	
						echo "<hr>";
                        echo "<b>$username<b>";
                     	echo "</br></br>";					
						// Turn off error reporting
						$instagram_url = "".$userid."/media/recent/?access_token=".$access_token;
						$instagram_json = file_get_contents($instagram_url);

						$instagram_array = json_decode($instagram_json,true);

							foreach($instagram_array['data'] as $image)
					    <div class="well">
						<img class="img-responsive" style="padding-left:35px;" src="<?php echo $image['images']['standard_resolution']['url'] ?>" alt="Instagram Photos">
						<p>Likes : <?php echo $image['likes']['count'] ?></p>




I used Some Bootstrap for giving it a basic look. You can customise it according to you. The sample output image is given below. 

instagram user image fetcher -



       You can demo the above app by visiting following link.

Similar Posts

Web development
22nd Jul 2018 01:47:06 AM
PHP CodeIgniter Laravel Bootstrap


ShareurCodes is a code sharing site for programmers to learn, share their knowledge with younger generation.