Get Images from Instagram using PHP

Of course, it is also possible to get Instagram feed using JavaScript as well.

As for me, the JavaScript method has two major advantages.

Ok, so, the post is about PHP connection to Instagram API, isnโ€™t it? ๐Ÿ™ƒ

Step 1. Instagram API cURL connection

First of all you have to insert this function somewhere into your code, otherwise none of the following examples will work. If you use WordPress insert the function to your current theme functions.php file.

function rudr_instagram_api_curl_connect( $api_url ){
	$connection_c = curl_init(); // initializing
	curl_setopt( $connection_c, CURLOPT_URL, $api_url ); // API URL to connect
	curl_setopt( $connection_c, CURLOPT_RETURNTRANSFER, 1 ); // return the result, do not print
	curl_setopt( $connection_c, CURLOPT_TIMEOUT, 20 );
	$json_return = curl_exec( $connection_c ); // connect and get json data
	curl_close( $connection_c ); // close connection
	return json_decode( $json_return ); // decode and return

Step 2. Instagram Access Token

Instagram API doesnโ€™t work with Client ID anymore, now every API connection requires Access Token.

Step 3. Get posts / photos from Instagram

According to the latest Instagram API changes, until you approve your application you can get only the latest 20 media of the access token owner. It means that only the one example from this post will work good and only if you specify your own username.

By a tag

In case you want to use a lightbox plugin, you should place the URL to the maximum size image into the link href attribute โ€” $post->images->standard_resolution->urlcontains the URL to 612ร—612 image (the max resolution in Instagram).

$access_token = 'YOUR ACCESS TOKEN';
$tag = 'wordcamprussia2015';
$return = rudr_instagram_api_curl_connect('' . $tag . '/media/recent?access_token=' . $access_token);
//var_dump( $return ); // if you want to display everything the function returns
foreach ( $return->data as $post ) {
	echo '<a href="' . $post->images->standard_resolution->url . '"><img src="' . $post->images->thumbnail->url . '" /></a>';
	$post->images->standard_resolution->url - URL of 612x612 image
	$post->images->low_resolution->url - URL of 150x150 image
	$post->images->thumbnail->url - URL of 306x306 image
	$post->type - "image" or "video"
	$post->videos->low_resolution->url - URL of 480x480 video
	$post->videos->standard_resolution->url - URL of 640x640 video
	$post->link - URL of an Instagram post
	$post->tags - array of assigned tags
	$post->id - Instagram post ID
	$post->filter - photo filter
	$post->likes->count - the number of likes to this photo
	$post->comments->count - the number of comments

By a username

Instagram API allows to get user feed only by a user ID. It can mean only the one thing โ€” we should get the User ID at first.

$access_token = 'YOUR ACCESS TOKEN';
$username = 'rudrastyh';
$user_search = rudr_instagram_api_curl_connect("" . $username . "&access_token=" . $access_token);
// $user_search is an array of objects of all found users
// we need only the object of the most relevant user - $user_search->data[0]
// $user_search->data[0]->id - User ID
// $user_search->data[0]->first_name - User First name
// $user_search->data[0]->last_name - User Last name
// $user_search->data[0]->profile_picture - User Profile Picture URL
// $user_search->data[0]->username - Username
$user_id = $user_search->data[0]->id; // or use string 'self' to get your own media
$return = rudr_instagram_api_curl_connect("" . $user_id . "/media/recent?access_token=" . $access_token);
//var_dump( $return ); // if you want to display everything the function returns
foreach ($return->data as $post) {
	echo '<a href="' . $post->images->standard_resolution->url . '"><img src="' . $post->images->thumbnail->url . '" /></a>';

Returned Values

Both /users/{ID OF A USER}/media/recent and /tags/{NAME OF A TAG}/media/recent return the same array of objects. View the examples above.

Get Instagram Profile Picture using PHP

You probably understood how to do it from my previous example, but anyway I would like to highlight this moment.

In case you want to do it, you have to use the following endpoint /users/search?q={USER ID}, example:

$access_token = 'YOUR ACCESS TOKEN';
$username = 'rudrastyh';
$user_search = rudr_instagram_api_curl_connect("" . $username . "&access_token=" . $access_token);

echo '<img src="' . $user_search->data[0]->profile_picture . '" />;

This endpoint allows you to display not only a profile picture of a user, you can use print_r( $user_search->data[0] ) to find out what else.


Misha Rudrastyh

Misha Rudrastyh

Hey guys and welcome to my website. For more than 10 years I've been doing my best to share with you some superb WordPress guides and tips for free.

Need some developer help? Contact me

Follow me on X