Skip to content

Instantly share code, notes, and snippets.

@nclsjstnn
Created November 19, 2013 14:40
Show Gist options
  • Save nclsjstnn/7546317 to your computer and use it in GitHub Desktop.
Save nclsjstnn/7546317 to your computer and use it in GitHub Desktop.
Get a list of instagrams refereed to a tag
<?php
function fetchData($url){
//edit here
$search = "mercadoconvite";
$access_token = "TU_TOKEN";
$count = "14";
//LET IT BE
$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;
}
$result = fetchData("https://api.instagram.com/v1/tags/".$search."/media/recent/?access_token=".$access_token."&count=".$count);
$cache = './instagram.json';
if(file_exists($cache) && filemtime($cache) > time() - 60*60){
// If a cache file exists, and it is newer than 1 hour, use it
$instaData = json_decode(file_get_contents($cache));
} else {
$instaData = json_decode((file_get_contents($result)));
file_put_contents($cache,json_encode($instaData));
}
foreach ($instaData->data as $post) {
if(empty($post->caption->text)) {
// Do Nothing
}
else {
echo '<a class="instagram-unit" target="blank" href="'.$post->link.'">
<img src="'.$post->images->low_resolution->url.'" alt="'.$post->caption->text.'" width="100%" height="auto" />
<div class="instagram-desc">'.htmlentities($post->caption->text).' | '.htmlentities(date("F j, Y, g:i a", $post->caption->created_time)).'</div></a>';
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment