Get Images from Instagram using PHP

PHP code examples of how to get posts from Instagram by a username or by a tag. With new API support.

/54 comments

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. Do not be afraid — you can easily get it using this tool.

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.

If you want to display Instagram feed without an access token, you can download my plugin for WordPress.

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('https://api.instagram.com/v1/tags/' . $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
	$post->caption->text
	$post->created_time
 
	$post->user->username
	$post->user->profile_picture
	$post->user->id
 
	$post->location->latitude
	$post->location->longitude
	$post->location->street_address
	$post->location->name
	*/
 
}

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("https://api.instagram.com/v1/users/search?q=" . $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("https://api.instagram.com/v1/users/" . $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("https://api.instagram.com/v1/users/search?q=" . $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.

Related

Misha Rudrastyh

Misha Rudrastyh

I love WordPress, WooCommerce and Gutenberg so much. 10 yrs of experience.

Need some custom developer help? Let me know

Follow Misha

Need some help with WordPress?

If you need some professional developer help, I will be happy to assist you.

Contact me Who I am?

Comments — 54

Leave a comment

php js HTML CSS Code