Skip to content

Instantly share code, notes, and snippets.

@raamdev
Created October 6, 2012 05:49
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 raamdev/3844131 to your computer and use it in GitHub Desktop.
Save raamdev/3844131 to your computer and use it in GitHub Desktop.
Load a Flickr photo set and then display a random photo from that set
<?php
/**
* Raam Dev - http://raamdev.com/
*
* This code loads a Flickr Photo Set and then picks a random photo from that set
* After configuring your settings and loading the PHP, you can display the random photo as follows:
* <a href="<?php echo $flickr_img_url_large; ?>" title="<?php echo $flickr_photo_description; ?>">
* <img title="<?php echo $flickr_photo_description; ?>" border="0" src="<?php echo $flickr_img_url; ?>" />
* </a>
*
* See the home page on http://raamdev.com/ to see this code in action.
*/
// Set your Flickr API Key, User ID, and Flickr Album ID
$flickr_api_key = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'; // Your own API key (get one here: http://www.flickr.com/services/apps/create/apply/)
$flickr_user_id = '89743353@N00'; // Your numeric user ID. Look up your ID here: http://idgettr.com/
$flickr_album_id = '72157626450096422'; // ID of the photoset; you'll see this in the URL when you visit the photoset on Flickr
// Get a list of all photos in specified photoset and load the result into an array
$flickr_list_of_photos = "http://api.flickr.com/services/rest/?method=flickr.photosets.getPhotos&api_key=$flickr_api_key&photoset_id=$flickr_album_id";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $flickr_list_of_photos);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$flickr_list_of_photos_xml = curl_exec($ch);
curl_close($ch);
$flickr_photos = simplexml_load_string($flickr_list_of_photos_xml);
// Get the last item in the array, since it's the newest photo in the set
$flickr_item = rand(0, count($flickr_photos->photoset->photo) - 1);
// Get the data for that photo so that we can build the image URL
$flickr_photo_id = $flickr_photos->photoset->photo[$flickr_item][id];
$flickr_secret = $flickr_photos->photoset->photo[$flickr_item][secret];
$flickr_server = $flickr_photos->photoset->photo[$flickr_item][server];
$flickr_farm = $flickr_photos->photoset->photo[$flickr_item][farm];
$flickr_title = $flickr_photos->photoset->photo[$flickr_item][title];
// Build URL to the photo (appending _b to the filename gets the large size)
// See here for more on building URLs: http://www.flickr.com/services/api/misc.urls.html
$flickr_img_url = "http://farm$flickr_farm.static.flickr.com/$flickr_server/" . $flickr_photo_id . "_" . $flickr_secret . ".jpg";
$flickr_img_url_large = "http://farm$flickr_farm.static.flickr.com/$flickr_server/" . $flickr_photo_id . "_" . $flickr_secret . "_b.jpg";
// Get information on the photo, including description
$flickr_photo_info_xml_url = "http://api.flickr.com/services/rest/?method=flickr.photos.getInfo&api_key=$flickr_api_key&photo_id=$flickr_photo_id";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $flickr_photo_info_xml_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$flickr_photo_info_xml = curl_exec($ch);
curl_close($ch);
$flickr_photo_info = simplexml_load_string($flickr_photo_info_xml);
// Extract the description; if there is no description, use the title instead
$flickr_photo_description = $flickr_photo_info->photo->description;
if ('' == $flickr_photo_description) { $flickr_photo_description = $flickr_photo_info->photo->title; }
/**
* Now you can display the random photo with some HTML like this:
* <a href="<?php echo $flickr_img_url_large; ?>" title="<?php echo $flickr_photo_description; ?>"><img title="<?php echo $flickr_photo_description; ?>" border="0" src="<?php echo $flickr_img_url; ?>"></a>
*/
?>
@aldolammel
Copy link

Dont run :(

Copy link

ghost commented Nov 4, 2017

PHP?, seems JS would be ideal.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment