How to connect Instagram API using wp_remote_get() function in WordPress

Everytime when I worked with Instagram API I used either jQuery ajax or cURL. But now for each GET call to Instagram API I use wp_remote_get() WordPress function.

So, you may go to API endpoints page and if you see GET near the endpoint it means that it can be implemented via wp_remote_get().

Instagram API endpoints. All GET-endpoints can be called via wp_remote_get() WP function.

For POST-endpoints you could use wp_remote_post(), but my app is in Sandbox Mode, so I have no chance to test it and I don’t mention untested examples in my posts.

Now let’s look at some examples that can be easily implemented for Sandbox Mode apps.

Example 1. Get information about the Instagram user via wp_remote_get()

You do not have to approve your app to get profile information of an access token owner. And it is really good news because many companies and people asks us, the developers to display the number of their Instagram subscribers.

Below is the example of getting ALL the user information from Instagram.


$your_token = 'Access token is required, read above how to obtain it';

// if your app is not approved - always use 'self'
$ig_user_id = 'self';

// Instagram API connection
$remote_wp = wp_remote_get( "https://api.instagram.com/v1/users/" . $ig_user_id . "/?access_token=" . $your_token );

// Instagram response is JSON encoded, let's convert it to an object
$instagram_response = json_decode( $remote_wp['body'] );

// 200 OK
if( $remote_wp['response']['code'] == 200 ) {
	// $instagram_response->data object contains all the user information
	echo '
	

' . $instagram_response->data->username . ' ID: ' . $instagram_response->data->id . '

' . $instagram_response->data->full_name . ' ' . $instagram_response->data->bio . '

' . $instagram_response->data->website . '
Media: ' . $instagram_response->data->counts->media . ' Subscribers: ' . $instagram_response->data->counts->followed_by . ' Subscribed: ' . $instagram_response->data->counts->follows . '
'; // Example of error handling, try to set incorrect user ID to catch an error // 400 Bad Request } elseif ( $remote_wp['response']['code'] == 400 ) { echo '' . $remote_wp['response']['message'] . ': ' . $instagram_response->meta->error_message; }

Here is the Instagram API response example, taken from the official website:

Instagram API response example

Example 2. Use wp_remote_get() function to fetch the recent media of a user

This example may look even more simple than the previous one.

Yes, the Sandbox Mode allows you to get your own latest media (to say exactly — 20 recent media of the access token owner).


$your_token = 'YOUR ACCESS TOKEN HERE'; // read above how to get it

// I recommend to use "self" if your application is not approved
$ig_user_id = 'self';

$remote_wp = wp_remote_get( "https://api.instagram.com/v1/users/" . $ig_user_id . "/media/recent/?access_token=" . $your_token );

$instagram_response = json_decode( $remote_wp['body'] );

if( $remote_wp['response']['code'] == 200 ) {
	
	foreach( $instagram_response->data as $m ) {
	
		echo '
			
		      ';
		// more parameters here https://www.instagram.com/developer/endpoints/users/#get_users_media_recent

	}
	
} elseif ( $remote_wp['response']['code'] == 400 ) {
	echo '' . $remote_wp['response']['message'] . ': ' . $instagram_response->meta->error_message;
}

Is there a chance to get media by tags or by location?

Actually yes, but if your application is in Sandbox Mode, your results will be restricted by the 20 recent media of the token owner. So, only if there are tagged media among those 20 items, they will be displayed.

The same mechanism applies for getting photos/videos by a location.

If your app is not approved and you want to avoid these restrictions, please try my plugin. It doesn’t require an Access Token at all.

But how to get media by tags or by location if your application is approved?

It is easy enough — you could use the code from Example 2, just change the API endpoint for tags to:


/tags/{TAG NAME HERE}?access_token=ACCESS-TOKEN

And for locations to:


/locations/{LOCATION ID HERE}/media/recent?access_token=ACCESS-TOKEN
Misha Rudrastyh

Misha Rudrastyh

I develop websites since 2008, so it is total of 13 years of experience, oh my gosh. Most of all I love love love to create websites with WordPress and Gutenberg, some ideas and thoughts I share throughout my blog.

Need some developer help? Contact me