Getting Images from Instagram Using PHP
Photo by Souvik Banerjee On Unsplash
PHP is an open-source programming language and is very popular among web developers because it is easy to learn and use, one of which is to get Instagram images using PHP.
Currently, PHP is used in many popular websites such as Facebook, Wikipedia, WordPress. Besides PHP, of course, there is another way to retrieve Instagram feeds, which is by using JavaScript.
- Asynchronous AJAX requests do not affect the website’s response time.
- If you’re using PHP caching (such as the WP Super Cache plugin for WordPress), the JavaScript method helps you avoid caching the feed.
Alright, so this post is about connecting PHP to the Instagram API, and here are the steps:
First, you must add this function somewhere in your code; otherwise, none of the following examples will work. If you are using WordPress, add the function to your theme’s 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
}
The Instagram API no longer works with a Client ID; now, every API connection requires an Access Token.
According to the latest changes to the Instagram API, until your app is approved, you can only retrieve the latest 20 media items from the access token owner. This means only one example of this post will work properly, and only if you specify your own username.
By Tag
If you want to use a lightbox plugin, you should place the URL of the full-size image in the href attribute — $post > images -> standard_resolution -> url contains the URL to a 612×612 image (the maximum resolution on 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 Username
The Instagram API allows you to get a user’s feed only by their User ID. To do that, you first need to retrieve the User ID.
$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_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);
foreach ($return->data as $post) {
echo '<a href="' . $post->images->standard_resolution->url . '"><img src="' . $post->images->thumbnail->url . '" /></a>';
}
Returned Data
Both endpoints: /users/{USER ID}/media/recent and /tags/{TAG NAME}/media/recent return the same array of objects. Please refer to the examples above for details.
You might have figured out how to do this from the previous example, but here we want to specifically highlight this point.
To do this, you should 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 not only allows you to display the user’s profile picture, but you can also use print_r( $user_search->data[0] ) to find out more available data.
That’s the article on Getting Images from Instagram Using PHP that Mangcoding could share. Please try to practice what we’ve explained in the article above. Hopefully, this article is useful, helps you solve your problems, and becomes a solution for you.
Reference: : Getting Images from Instagram Using PHP