Get Images from Instagram with PHP

In this tutorial I will show you how you can interact with Instagram API in PHP and not only that – we’re also going to create and configure an app in “Meta for Developers”.

getting images from instagram with php

If you need any help with developing your custom web application which is going to interact with Instagram API, feel free to contact me and the team.

1. Create and Configure an Application

Creating an app

First of all visit this link developers.facebook.com/apps and hit a “Create App” button.

Select “Other”:

creating an app in Meta platform for Developers

When selecting app type, choose “Consumer”:

select an app type in meta for developers

Facebook will also ask you some details like contact email and application name and that’s pretty much it, the app is created!

Add platform

We are going to display photos from Instagram on a website, right? So let’s add this information to the app.

Go to App Settings > Basic, scroll to the very bottom of the page and hit “Add platform” button.

adding platform in app settings

Don’t forget to provide a website URL and then hit “Save changes”.

Set up “Instagram Basic Display” product

It is kind of a child app to our parent app 😁

All right, from the dashboard select “Instagtam Basic Display”:

instagram basic display product

Once you created this “child app”, you will have the access to “Instagram App ID”, “Instagram App Secret” and “Valid OAuth Redirect URIs” setting. We are going to use all of those in our API interaction in the examples below.

instagram app ID, instagram app secretn and redirect URLs

In “Valid OAuth Redirect URIs” please provide exact URL of the page where we are going to display images from Instagram. If you don’t do that, in this step you will get “Invalid redirect_uri” error.

Add a test user and accept invitation from Instagram

Go to App roles > Roles and hit “App people” button.

how to add Instagram Tester user

Now it is time to confirm it in your Instagram account, in order to do so please go to instagram.com/accounts/manage_access then “Tester Invitations” tab and accept the invitation.

Instagram acept test user invitation

If you skip this step or do it incorrectly, you will get an error “Insufficient developer role” in the next step.

2. OAuth Authentication

Finally we’re done with the app and it is time to code something. And in this first OAuth step all we need to do is just to redirect users to the following URL:

https://api.instagram.com/oauth/authorize?client_id={Instagram APP ID}&redirect_uri={Redirect URI}&scope=user_profile,user_media&response_type=code

Let’s create a HTML link for that. I am also going to use a handy WordPress function add_query_arg() which will help me to add query parameters to the URL.

<?php
$redirect = add_query_arg(
	array(
		'client_id' => '0000000012345678',
		'redirect_uri' => 'https://rudrastyh.com/instagram',
		'scope' => 'user_profile,user_media',
		'response_type' => 'code',
	),
	'https://api.instagram.com/oauth/authorize'
);
?>
<a href="<?php echo $redirect ?>">Show Instagram Photos</a>

If you’ve done everything correctly, when you click the link, you should be redirected to Instagram website and the following OAuth dialog should appear:

Instagram OAuth dialog

When you hit “Allow” button, you’re going to be redirected to the URL provided in redirect_uri parameter. Also ?code URL parameter will be added at the end. In our case it is going to be something like https://rudrastyh.com/instagram?code=AQvhrlcor...#_. We are about to use code in the following step to get an access token.

3. Get Instagram Access Token

Now it is time to get an Instagram access token. The request structure is going to be like this:

curl -X POST \
  https://api.instagram.com/oauth/access_token \
  -F client_id={Instagram App ID} \
  -F client_secret={Instagram App Secret} \
  -F grant_type=authorization_code \
  -F redirect_uri={Redirect URI} \
  -F code={Code}

Since I am using PHP and this blog is pretty much about WordPress, for convenience I am going to use WordPress HTTP API functions, in this case – wp_remote_post().

if( isset( $_GET[ 'code' ] ) && $_GET[ 'code' ] ) {
	$response = wp_remote_post(
		'https://api.instagram.com/oauth/access_token',
		array(
			'body' => array(
				'client_id' => '0000000012345678',
				'client_secret' => '0000000000000000',
				'grant_type' => 'authorization_code',
				'redirect_uri' => 'https://rudrastyh.com/instagram',
				'code' => $_GET[ 'code' ],
			),
		)
	);

	if( 'OK' === wp_remote_retrieve_response_message( $response ) ) {
		$response = json_decode( wp_remote_retrieve_body( $response ), true );
		$access_token = $response[ 'access_token' ];
	}
}

Please note, that the authorization code we get from $_GET[ 'code' ] can be used only once to get an access token, if you try to use it one more time, you will get an error “This authorization code has been used”.

Generate long-term access tokens

Please note, that the access token we generated above is a short-term one. It can be used only for 1 hour, after that it is getting expired. But, using the short-term token, you can generate a long-term (valid for 60 days) one with an additional API request.

curl -i -X GET 'https://graph.instagram.com/access_token?grant_type=ig_exchange_token&client_secret={Instagram App Secret}&access_token={Access Token}'

Or in a WordPress-way:

$response = wp_remote_get(
	add_query_arg(
		array(
			'grant_type' => 'ig_exchange_token',
			'client_secret' => '0000000000000000',
			'access_token' => $access_token,
		),
		'https://graph.instagram.com/access_token'
	)
);
if( 'OK' === wp_remote_retrieve_response_message( $response ) ) {
	$response = json_decode( wp_remote_retrieve_body( $response ), true );
	$access_token = $response[ 'access_token' ];
}

4. Get Images from Instagram

Finally, it is time to print some images from our Instagram feed!

curl -X GET 'https://graph.instagram.com/me/media?fields=media_url&access_token={Access Token}'
$response = wp_remote_get(
	add_query_arg(
		array(
			'fields' => 'media_url',
			'access_token' => $access_token,
		),
		'https://graph.instagram.com/me/media'
	)
);
if( 'OK' === wp_remote_retrieve_response_message( $response ) ) {
	$response = json_decode( wp_remote_retrieve_body( $response ), true );
	foreach( $response[ 'data' ] as $media ) {
		echo '<img src="' . $media[ 'media_url' ] . '" />';
	}
}
getting images from instagram with php

Media fields

In the example above the only thing we get from Instagram API is the media URL. But sometimes when you get Instagram posts, you need more than that.

  • caption,
  • id,
  • media_type – can be IMAGE, VIDEO and CAROUSEL_ALBUM,
  • media_url,
  • permalink – link on Instagram,
  • thumbnail_url – thumbnail for VIDEO type,
  • timestamp – date of the publication,
  • username – username of the author.

You can provide multiple fields comma separated like this:

$response = wp_remote_get(
	add_query_arg(
		array(
			'fields' => 'media_url,permalink,timestamp',
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