SahilMepani
1/30/2019 - 4:28 AM

PHP: Get Instagram Feeds

<!-- Helper Funcion -->
function get_instagram_feed( $max_images = 10 ) { // max 33 allowed https://rudrastyh.com/instagram/more-than-33.html
  //Variables cannot be empty. Replace with app details
  $user_id = '2796811886';
  $access_token = '2796811886.1677ed0.f30b6326b0474fd79447aac899d2a167';
  $max_images = $max_images; //image count

  $instaResult = get_transient( 'instagram_feed_trans' );

  if ( false === $instaResult || $instaResult == "") {
  	$instaResult = file_get_contents("https://api.instagram.com/v1/users/" . $user_id . "/media/recent/?access_token=" . $access_token . "&count=" . $max_images);
  	set_transient( 'instagram_feed_trans', $instaResult, 1 * HOUR_IN_SECONDS );
  }
  return json_decode($instaResult);
}


<!-- Load the feeds -->
<?php
		  $insta_feeds = get_instagram_feed( 10 );
		  if ( $insta_feeds ) :
		  foreach ( $insta_feeds->data as $feed ) :
		  	// var_dump($feed);
				$link        = $feed->link;
				$image       = $feed->images->standard_resolution->url;
				$video       = $feed->videos->standard_resolution->url;
				$alt         = $feed->caption->text;
				$description = htmlentities($feed->caption->text);
				$date        = htmlentities(date("F j, Y, g:i a", $feed->caption->created_time));
		?>

				<li class="feed">
					<div class="img-block" style="background-image: url(<?php echo $image; ?>);"></div>
		    </li>

		<?php endforeach; ?>
		<?php endif; ?>