Get Photos from Instagram via PHP

PHP code samples of getting media from Instagram by a username or by a tag. With new API support.


Of course, such lightbox viewers like Fancybox can be easily used if you get Instagram feed via 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. Prepare. Function to set up cURL connection to Instagram API

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. Still Prepare. 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 new Instagram API changes, until you approve your application you can get only the latest 20 media of youself. It means that only the one example from this post will work good and only if you specify your own username.

If you don’t want to deal with the code and access tokens, you can download ready to use plugin for WordPress. It doesn’t require an access token and has no restrictions.

By a tag

As we want to use Fancybox plugin (or another lightbox) we should place the URL to the maximum size image into the link href attribute — $post->images->standard_resolution->url contains 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 . '" class="fancybox"><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 . '" class="fancybox"><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.

More about Instagram API

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 — 60

Leave a comment

php js HTML CSS Code