Skip to content

Instantly share code, notes, and snippets.

@danimalweb
Created January 4, 2017 05:17
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danimalweb/fb4e8a4c12ff8e1376215014e52d4bdb to your computer and use it in GitHub Desktop.
Save danimalweb/fb4e8a4c12ff8e1376215014e52d4bdb to your computer and use it in GitHub Desktop.
Instagram Curl API
<?php
function fetchData($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
// delete_transient('latest_instagram'); //Used to clear the transient if we change something
if (false === ( $result = get_transient('latest_instagram') ) ) { //Get the data and assign to result if false run the code
$result = fetchData("https://api.instagram.com/v1/users/CHANGEME/media/recent/?access_token=XXXX&count=8");
$result = json_decode($result);
if (is_object($result)) {
set_transient('latest_instagram', $result, 60*60*2); //Setup the Transient. Expires in 2 hours
}
}
//print_r($result);
if (is_object($result)) {
foreach ($result->data as $post) { ?>
<div class="col-xs-6 col-sm-3 no-padding">
<a href="<?php echo $post->link; ?>" title="View on Instragram" class="img-link">
<img src="<?php echo $post->images->low_resolution->url; ?>" height="100%" width="100%" alt="<?php $post->caption->text; ?>">
</a>
</div><?php
}
} ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment