<?

// Kosso: 31/01/07
// 'flickoffr.php' version 0.

// A simple script to get the urls of flickr photos given a username
// It uses the flickr API to first obtain the flickr user_id, then again to
// get all the photos and info from this user

// This could be extended easily to actually download your photos to your server, or desktop



// IMPORTANT: get a flickr API key from here [ http://www.flickr.com/services/api/keys/ ]
$api_key "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";

$count 0;

// ######## Ask for the username
if(!isset($_POST['username'])){

    echo 
"<form action=\"".$_SERVER['PHP_SELF']."\" method=\"POST\">";
    echo 
"enter your flickr 'screen name' <input type=\"text\" name=\"username\"><input type=\"Submit\" value=\"search for flickr photos by this user\">";
    echo 
"</form>";
    exit;
} else {

    
getFlickrPhotos($_POST['username']);

}



function 
getFlickrPhotos($username,$page=1){

    global 
$api_key$count;

    
// ######## Get user_id given username

    // ######## Build the API request to look up the flickr user_id
    
$params = array(
        
'api_key'    => $api_key,
        
'method'    => 'flickr.people.findByUsername',
        
'username'    => $username
    
);

    
$encoded_params = array();
    foreach (
$params as $k => $v){
        
$encoded_params[] = urlencode($k).'='.urlencode($v);
    }

    
$url "http://api.flickr.com/services/rest/?".implode('&'$encoded_params);

    
// As some PHP hosts switch off file_get_contents, here we check to see if curl is installed and use that instead.

    
if(function_exists("curl_init")){
        
$ch=curl_init($url);
        
curl_setopt($chCURLOPT_HEADERfalse);
        
curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
        
$rsp=curl_exec($ch);
        
curl_close($ch);
    } else {
        
$rsp file_get_contents($url);
    }


    
$userdetails simplexml_load_string($rsp);

    
// ######## If no user is found
    
if($userdetails['stat']=="fail"){
        echo 
"<b>no such flickr user found</b> - try again<br />";
        echo 
"<form action=\"".$_SERVER['PHP_SELF']."\" method=\"POST\">";
        echo 
"enter flickr username <input type=\"text\" name=\"username\"><input type=\"Submit\" value=\"search for flickr photos by this user\">";
        echo 
"</form>";
        exit;

    }

    
$user_id $userdetails->user['id'];
    if(
$page==1){
        echo 
"flickr userid for ".$_POST['username'].": "$user_id."<br />";
    }

    
// ######## Get photos, given user_id

    
$per_page 100;

    
// ######## Build the API request to search for photos by this user

    
$params = array(
        
'api_key'    => $api_key,
        
'method'    => 'flickr.photos.search',
        
'user_id'    => $user_id,
        
'page'        => $page,
        
'per_page'    => $per_page
    
);

    
$encoded_params = array();
    foreach (
$params as $k => $v){
        
$encoded_params[] = urlencode($k).'='.urlencode($v);
    }

    
$url "http://api.flickr.com/services/rest/?".implode('&'$encoded_params);

    if(
function_exists("curl_init")){
        
$ch=curl_init($url);
        
curl_setopt($chCURLOPT_HEADERfalse);
        
curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
        
$rsp=curl_exec($ch);
        
curl_close($ch);
    } else {
        
$rsp file_get_contents($url);
    }

    
$simplephotos simplexml_load_string($rsp);

    
// ######## Check if any photos were found
    
if($simplephotos['stat']=="fail"){
        echo 
"<b>no photos found</b> - try again<br />";
        echo 
"<form action=\"".$_SERVER['PHP_SELF']."\" method=\"POST\">";
        echo 
"enter flickr username <input type=\"text\" name=\"username\"><input type=\"Submit\" value=\"search for flickr photos by this user\">";
        echo 
"</form>";
        exit;
    }

    
$totalCount $simplephotos->photos['total'];

    
// ######## list the photos
    
if($page==1){
        echo 
"Found a total of ".$totalCount." photos<br />";
    }

    echo 
"Page ".$page." of ".ceil($simplephotos->photos['total']/$per_page)."<br />";
    echo 
"<hr>";

    foreach (
$simplephotos->photos->photo as $photo) {

        
$count ++;
        
// the urls to the different photo sizes are built like this:
        // http://farm#FARM#.static.flickr.com/#SERVER#/#ID#_#SECRET#_o_d.jpg      // original
        // http://farm#FARM#.static.flickr.com/#SERVER#/#ID#_#SECRET#_d.jpg        // medium
        // http://farm#FARM#.static.flickr.com/#SERVER#/#ID#_#SECRET#_m_d.jpg    // small
        // http://farm#FARM#.static.flickr.com/#SERVER#/#ID#_#SECRET#_t_d.jpg    // thumbnail
        // http://farm#FARM#.static.flickr.com/#SERVER#/#ID#_#SECRET#_s_d.jpg    // square

        // where #VALUE# are the attributes of each 'photo' element in the XML reply

        // title
        
echo "<b>".$photo['title']."</b><br />";

        
// display the thumbnail
        
echo "<img src=\"http://farm".$photo['farm'].".static.flickr.com/".$photo['server']."/".$photo['id']."_".$photo['secret']."_s.jpg\"><br clear=\"left\" />";

        
// original photo url with link
        
echo "<a href=\"http://farm".$photo['farm'].".static.flickr.com/".$photo['server']."/".$photo['id']."_".$photo['secret']."_o_d.jpg\">http://farm".$photo['farm'].".static.flickr.com/".$photo['server']."/".$photo['id']."_".$photo['secret']."_o_d.jpg</a>";
        echo 
"<br clear=\"left\" />";

        
// ############# Get the info about this photo - tags, geodata, comments etc

        
$tparams = array(
            
'api_key'    => $api_key,
            
'method'    => 'flickr.photos.getInfo',
            
'photo_id'    => $photo['id']
        );
        
$encoded_tparams = array();
        foreach (
$tparams as $k => $v){
            
$encoded_tparams[] = urlencode($k).'='.urlencode($v);
        }

        
$url "http://api.flickr.com/services/rest/?".implode('&'$encoded_tparams);
        if(
function_exists("curl_init")){
            
$ch=curl_init($url);
            
curl_setopt($chCURLOPT_HEADERfalse);
            
curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
            
$rsp=curl_exec($ch);
            
curl_close($ch);
        } else {
            
$rsp file_get_contents($url);
        }

        
$info simplexml_load_string($rsp);

        if(
$info['stat']=="fail"){
            echo 
"there was an error getting the info for this photo<br />";
            exit;
        }

        
// date uploaded
        
echo "uploaded : ".date("r",(string)$info->photo['dateuploaded'])."<br />";

        
// ##### DESCRIPTION
        
if((string)$info->photo->description!=""){
            echo 
"description :".(string)$info->photo->description."<br />";
        }

        
// ###### TAGS
        
if(count($info->photo->tags->children()) > 0){

            echo 
"tags : ";
            foreach(
$info->photo->tags->children() as $tag){

                echo 
$tag." ";

            }
            echo 
"<br />";

        }

        
// ######## GEO DATA
        
if(isset($info->photo->location)){
            echo 
"Geo location : Lat:".$info->photo->location['latitude']."  Long:".$info->photo->location['longitude']."<br />";
        }

        
// #### COMMENTS
        
if((string)$info->photo->comments 0){
            
// ######### Get comments for this photo
            
$cparams = array(
                
'api_key'    => $api_key,
                
'method'    => 'flickr.photos.comments.getList',
                
'photo_id'    => $photo['id']
            );
            
$encoded_cparams = array();
            foreach (
$cparams as $k => $v){
                
$encoded_cparams[] = urlencode($k).'='.urlencode($v);
            }

            
$url "http://api.flickr.com/services/rest/?".implode('&'$encoded_cparams);
            if(
function_exists("curl_init")){
                
$ch=curl_init($url);
                
curl_setopt($chCURLOPT_HEADERfalse);
                
curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
                
$rsp=curl_exec($ch);
                
curl_close($ch);
                
//echo $data;
            
} else {
                
$rsp file_get_contents($url);
            }

            
$comments simplexml_load_string($rsp);

            if(
$comments['stat']=="fail"){
                echo 
"there was an error getting the comments for this photo<br />";
                exit;
            }
            foreach (
$comments->comments->comment as $comment) {
                echo 
"<li>[".$comment['authorname']."] ".$comment." (".date("r",(string)$comment['datecreate']).")";
            }

        }
        echo 
"<br clear=\"all\" />";

        
flush();

    }

    echo 
"count: ".$count."/".$totalCount."<hr>";

    if(
$count $totalCount){

        
$page++;
        
getFlickrPhotos($username,$page);

    } else {

        echo 
"<br />DONE";

        echo 
"<br />You would now want to code the download of YOUR PHOTOS to your server ;)
         then zipup, ftp, etc.. to where you need / want them
         Also, you might want to build you own system and enter all the juicy metadata into a database
        <br />Now, all we need is a way to export/inport these to another photo service like <a href=\"http://zooomr.com\">Zooomr</a>
        <br /> Cheers,
        <br /> Kosso"
;

    }

}

?>