DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Random Flickr Photo

04.11.2006
| 13513 views |
  • submit to reddit
        // description of your code here

<?php

    // Displays a single random photo from recent flickr photos with a given tag.
    // Original code stolen from many sources including http://www.thebishop.net/geodog/archives/2004/09/29/fun_hacking_with_flickr_making_a_homemade_flickr_tag_badge_with_magpierss.html and http://prwdot.org/archives/002468.html


    // USER CONFIGURATION SECTION

    // MagpieRSS Configuration
    // This is an example based on my system;
    // you will need to customize it for your system
    // and your preferences. You can remove it entirely
    // if you have done it elsewhere
    // refer to http://magpierss.sourceforge.net/
    require_once('/var/www/bradford/magpierss-0.61/rss_fetch.inc');
    error_reporting(E_ERROR);
    define(MAGPIE_CACHE_ON, true);
    define(MAGPIE_CACHE_DIR, '/var/www/bradford/magpie_cache');
    define(MAGPIE_CACHE_AGE, 300);
    define(MAGPIE_CACHE_FRESH_ONLY, false);
    define(MAGPIE_DETECT_ENCODING, true);
    define(MAGPIE_DEBUG, 0);
    define(MAGPIE_FETCH_TIME_OUT, 15);
    define(MAGPIE_USE_GZIP, true);    

    // flickr configuration
    // How many photos you want to display
    $num_photos = 2; // for some reason it doesn't like 1
    $tag = 'Bradford';
    // URL for the flickr feed you want to use
    $flickr_feed_url ='http://www.flickr.com/services/feeds/photos_public.gne?tags='.$tag.'&format=rss_200';
?>

<?php
    // Fetch the feed
    $flickr = fetch_rss( $flickr_feed_url );
    if ($flickr) {
        $flickr_title = $flickr->channel["title"];
        $flickr_link = $flickr->channel["link"];
?>

<!-- Display the title and link to the feed -->

<?php
    // Pick some random photos
        $random_photos = array_rand($flickr->items,$num_photos);
        foreach ( $random_photos as $random_photo ) {
            $description = explode("\n\n",$flickr->items[$random_photo]["description"]);
?>

    <!-- Display the given photo -->

	<?php echo ereg_replace('<img src=(.*) width=(.*)>', '<img src=\\1 width="150px"/>', $description[1]);
	  die();?> // ok we've got our first photo - lets exit

<?php
        }
      } else {
?>

<!-- Display an error message if things didn't work -->
<p>An error occurred in the MagpieRSS parser:</p>

<p><?php echo magpie_error(); ?></p>

<?php
    } 
?>