Skip to content

Instantly share code, notes, and snippets.

@marchawkins
Created February 8, 2013 06:21
Show Gist options
  • Save marchawkins/4737032 to your computer and use it in GitHub Desktop.
Save marchawkins/4737032 to your computer and use it in GitHub Desktop.
Display your photos from flickr on your website with php.
<?php
$albumParams = array(
'method' => 'flickr.photos.search',
'user_id' => 'YOURFLICKRUSERID', // find it at http://idgettr.com
'per_page' => '18'
);
$albumInfo = getFlickr($albumParams);
?>
<html>
<head>
<title>flickr photos</title>
<style>
body {font-family: verdana,helvetica,sans-serif; font-size: 9pt;}
ul {list-style: none;}
li {display: block; float: left; margin: 1%;}
</style>
</head>
<body>
<?php if(count($albumInfo["photos"]["photo"])>0) { ?>
<ul class="gallery" data-clearing>
<?php
foreach($albumInfo["photos"]["photo"] as $photo) {
$thumbSrc = "http://farm".$photo['farm'].".static.flickr.com/".$photo['server']."/".$photo['id']."_".$photo['secret']."_q.jpg";
$originalSrc = "http://farm".$photo['farm'].".static.flickr.com/".$photo['server']."/".$photo['id']."_".$photo['secret']."_z.jpg";
?>
<li><a href="<?= $originalSrc ?>" title="<?= $photo['title'] ?>"><img src="<?= $thumbSrc ?>" width="80" height="80"/></a></li>
<?php } ?>
<div style="clear:both;"></div>
</ul>
<?php } else {
/* if something goes wrong, display a message */ ?>
<p>Sorry, there was a problem. Try again later.</p>
<?php } ?>
</body>
</html>
<?php
// functions to interact with flickr
function getFlickr($params) {
if(count($params)>0) {
$params["api_key"] = "YOURAPIKEY";
$params["format"] = "php_serial";
$encoded_params = array();
foreach ($params as $k => $v){ $encoded_params[] = urlencode($k).'='.urlencode($v); }
$ch = curl_init();
$timeout = 5; // set to zero for no timeout
curl_setopt ($ch, CURLOPT_URL, 'http://api.flickr.com/services/rest/?'.implode('&', $encoded_params));
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$file_contents = curl_exec($ch);
curl_close($ch);
$flickrInfo = unserialize($file_contents);
return $flickrInfo;
} else {
return false;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment