Skip to content

Instantly share code, notes, and snippets.

@thetwopct
Created December 22, 2018 05:52
Show Gist options
  • Save thetwopct/53a164121d396eb0331c75eadfc0a009 to your computer and use it in GitHub Desktop.
Save thetwopct/53a164121d396eb0331c75eadfc0a009 to your computer and use it in GitHub Desktop.
PHP pull Instagram feed and display with cache
<?php
$cache_file = getcwd() . '/media/latest_gram.json';
$cache_life = '120'; //caching time, in seconds
if ( !file_exists($cache_file) or ( time() - filemtime($cache_file) >= $cache_life ) ) {
file_put_contents( $cache_file, file_get_contents('https://api.instagram.com/v1/users/self/media/recent/?access_token=xxx&count=1') );
}
$json = file_get_contents( $cache_file );
$ig_array = json_decode( $json );
?>
<a target="_blank" href="<?php echo $ig_array->data[0]->link ?>"><img src="<?php echo $ig_array->data[0]->images->low_resolution->url; ?>" /></a>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment