Skip to content

Instantly share code, notes, and snippets.

@swooningfish
Forked from owex/get-instagram-feed.php
Created November 10, 2017 14:11
Show Gist options
  • Save swooningfish/fb6efccc02d4e22559a4e9fccf835793 to your computer and use it in GitHub Desktop.
Save swooningfish/fb6efccc02d4e22559a4e9fccf835793 to your computer and use it in GitHub Desktop.
Simple PHP Instagram feed
<?php
// Created following https://medium.com/@bkwebster/how-to-get-instagram-api-access-token-and-fix-your-broken-feed-c8ad470e3f02
function get_instagram($user_id=XXXXXXXXX, $count=18, $width=190, $height=190){
$url = 'https://api.instagram.com/v1/users/'.$user_id.'/media/recent/?access_token=XXXXXXXXXXXXXXXXXXXXXX&count='.$count;
// Also Perhaps you should cache the results as the instagram API is slow
$cache = './instagram/'.sha1($url).'.json';
if(file_exists($cache) && filemtime($cache) > time() - 60*60){
// If a cache file exists, and it is newer than 1 hour, use it
$jsonData = json_decode(file_get_contents($cache));
} else {
$jsonData = json_decode((file_get_contents($url)));
file_put_contents($cache,json_encode($jsonData));
}
$result = '<div class="row no-gutters">'.PHP_EOL;
foreach ($jsonData->data as $key=>$value) {
$result .= "\t".'<div class="col-6 col-sm-3 col-md-3 col-lg-2"><a data-fancybox="group"
title="'.htmlentities($value->caption->text).' '.htmlentities(date("F j, Y, g:i a", $value->caption->created_time)).'"
href="'.$value->images->standard_resolution->url.'">
<img class="img-fluid" src="'.$value->images->low_resolution->url.'" alt="'.$value->caption->text.'" width="'.$width.'" height="'.$height.'" />
</a></div>'.PHP_EOL;
}
$result .= '</div>'.PHP_EOL;
return $result;
}
echo get_instagram();
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment