Skip to content

Instantly share code, notes, and snippets.

@davidsword
Last active February 3, 2018 03:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidsword/7c32c7d2d77928eeac9ecf629167b7fd to your computer and use it in GitHub Desktop.
Save davidsword/7c32c7d2d77928eeac9ecf629167b7fd to your computer and use it in GitHub Desktop.
<?
// Settings
$groups = array(
'71332142@N00', # catchy colours
'1949459@N23', # nikon
'83812269@N00', # nightlife
'52239829790@N01', # subsets
'52239733174@N01', # B&W
'42097308@N00', # less is more
'52240257802@N01', # long exposure
);
$flickrapi = '####################';
$cache = __DIR__."/flickr.cache"; // make this file in same dir
$force_refresh = false; // dev
$refresh = 60*60*24; // once a day
// cache api results so to not over-query (api restrictions)
if ($force_refresh || isset($_GET['refresh_flickr']) || ((time() - filectime($cache)) > ($refresh) || 0 == filesize($cache))) {
// were freshing the feed, select a random group from pool
$groupc = count($groups)-1;
$random_group = rand(0,$groupc);
// setup flickr url
$params = array(
'api_key' => $flickrapi,
'id' => $groups[$random_group],
'lang' => 'en-us',
'format' => 'php_serial',
);
$encoded_params = array();
foreach ($params as $k => $v)
$encoded_params[] = urlencode($k).'='.urlencode($v);
$url = "https://api.flickr.com/services/feeds/groups_pool.gne?".implode('&', $encoded_params);
// get api results
$ch = curl_init($url) or die("curl issue");
$curl_options = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => false,
CURLOPT_FOLLOWLOCATION => false,
CURLOPT_ENCODING => "",
CURLOPT_AUTOREFERER => true,
CURLOPT_CONNECTTIMEOUT => 7,
CURLOPT_TIMEOUT => 7,
CURLOPT_MAXREDIRS => 3,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_USERAGENT => "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/525.13 (KHTML, like Gecko) Chrome/0.A.B.C Safari/525.13"
);
curl_setopt_array($ch, $curl_options);
$curlcontent = curl_exec( $ch );
curl_close( $ch );
// write results to cache
$handle = fopen($cache, 'wb') or die('no fopen');
$flickr_cache = $curlcontent;
fwrite($handle, $flickr_cache);
fclose($handle);
} else {
$flickr_cache = file_get_contents($cache); //locally
}
$stream = unserialize($flickr_cache);
// show a random photo from the group (stream defaults at 20 items)
// change this var to `0` to keep a single image for $refresh duration
$show = rand(0,19);
echo $stream['items'][$show]['photo_url']."?date=".date('Y-m-d',strtotime($stream['items'][$show]['date_taken_nice']))."&group=".a_n($stream['title']);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment