
Latest photos from instagram on PHP
For display the latest photos from instagram on PHP, you need to write 2 functions. In the first, we turn to the instagram API:
// Get our data function fetchData( $url ){ $ch = curl_init(); curl_setopt( $ch, CURLOPT_URL, $url ); curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 ); curl_setopt( $ch, CURLOPT_TIMEOUT, 20 ); $result = curl_exec( $ch ); curl_close( $ch ); return $result; } |
In the second one – we set the number of photos and print the result on the page:
function instagram_feed( $instagram_token, $instagram_id ) { if ( $instagram_token != '' && $instagram_id != '' ){ // Pull and parse data. $result = fetchData( "https://api.instagram.com/v1/users/{$instagram_id}/media/recent/?access_token={$instagram_token}" ); $result = json_decode( $result ); $limit = 10; // Amount of images to show $i = 0; foreach ( $result->data as $post ){ if ( $i < $limit ){ echo '<a class="insta-wrap__link" href="' . $post->link . '" target="_blank" rel="nofollow noopener noreferrer">'; echo '<img class="insta-wrap__image" src="' . $post->images->thumbnail->url . '" alt="" />'; echo '</a>'; $i ++; } } } } |
In $post->images->thumbnail->url You can replace the ‘thumbnail’ with different values to get photos with different resolutions:
- thumbnail – 320*320px;
- low_resolution – 150*150px;
- standard_resolution – 640*640px;
And, finally, run the function where we need it:
<div class="instagram-feeds"> <?php $instagram_token = 'xxx'; $instagram_id = 'xxx'; if ( $instagram_token !='' && $instagram_id != '' ) { ?> <div class="insta-wrap"> <?php instagram_feed( $instagram_token, $instagram_id ); ?> </div> <?php } ?> </div> |
Variable $instagram_token – it’s Instagram Access Token (how to get it you can find out in this article. $instagram_id = you can find, for example, here. Just replace ‘xxx’ with your data and you will get a list of your latest photos from the instagram.
Full code:
<html lang="ru-RU" prefix="og: http://ogp.me/ns#" class="no-js"> <head> </head> <body> <?php // Get our data function fetchData( $url ){ $ch = curl_init(); curl_setopt( $ch, CURLOPT_URL, $url ); curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 ); curl_setopt( $ch, CURLOPT_TIMEOUT, 20 ); $result = curl_exec( $ch ); curl_close( $ch ); return $result; } function instagram_feed( $instagram_token, $instagram_id ) { if ( $instagram_token != '' && $instagram_id != '' ){ // Pull and parse data. $result = fetchData( "https://api.instagram.com/v1/users/{$instagram_id}/media/recent/?access_token={$instagram_token}" ); $result = json_decode( $result ); $limit = 10; // Amount of images to show $i = 0; foreach ( $result->data as $post ){ if ( $i < $limit ){ echo '<a class="insta-wrap__link" rel="nofollow noopener noreferrer" target="_blank" href="' . $post->link . '">'; echo '<img class="insta-wrap__image" src="' . $post->images->thumbnail->url . '" alt="">'; echo '</a>'; $i ++; } } } } ?> <div class="instagram-feeds"> <?php $instagram_token = 'xxx'; $instagram_id = 'xxx'; if ( $instagram_token !='' && $instagram_id != '' ) { ?> <div class="insta-wrap"> <?php instagram_feed( $instagram_token, $instagram_id ); ?> </div> <?php } ?> </div> </body> </html> |
