Flickr API and PHP, display images!

by Jonathan Christensen on Feb 08, 2010, 7:33 PM

Flickr in the past couple years has really turned into a great service. PHP too has been a vital item to internet development! Now lets make them play nice together to grab some of your recent images in your flikr account!

  1. The first step to get this working is to create a Flickr account.

  2. Once your account is created, get a API. You can do that by clicking this link: Create a Flickr API. It should be a 32 character (number and letter) random combination. This will be YOUR API so keep that somewhere easy to get to.

  3. At this point open your favorite development program (for me, CODA or Espresso) and FTP client. Create the file: flickr.class.php and apply the following code:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    
    <?php
    class flickr {
     
        function flickr($username,$api_key) {
            $this->username = $username;
            $this->api_key  = $api_key;
            $this->user_id  = '';
            $this->response = array();
            $this->timeout  = 5;
        }
     
        function getUserId() {
            $url = 'http://api.flickr.com/services/rest/?api_key=' . urlencode($this->api_key) . '&method=flickr.urls.lookupUser&url=http://www.flickr.com/photos/' . urlencode($this->username) . '/';
            $return = $this->fetch($url);
            return $return->user->attributes()->id;
        }
     
        function getImages($count) {
            $url = 'http://api.flickr.com/services/rest/?api_key=' . urlencode($this->api_key) . '&method=flickr.photos.search&user_id=' . $this->getUserId() . '&per_page=' . urlencode($count);
            return $this->fetch($url);
        }
     
        function fetch($url,$post = false) {
            $ch = curl_init($url);
            curl_setopt($ch, CURLOPT_NOBODY, 0);
            curl_setopt($ch, CURLOPT_HEADER, 0);
            curl_setopt($ch, CURLOPT_USERAGENT, 'Flickr');
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout);
            $output = curl_exec($ch);
            $this->response = curl_getinfo($ch);
            curl_close($ch);
            if((int)$this->response['http_code'] == 200) {
                return new SimpleXMLElement($output);
            }
            else {
                return false;
            }
        }
     
    }
    ?>

    The above code will be complicating if you do not understand PHP Classes and Functions. Hopefully you do understand enough of the syntax to breakdown some of the code.

  4. Next, enter into a page where you would want to display your photos! This is some example code, make sure your “flickr.class.php” require is looking at the correct location. After setting that, change where it says: “username” and “your-api-key”.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    
    <?php
     
    require_once('flickr.class.php'); 
     
    $flickr = new flickr('username','your-api-key');
    $images = $flickr->getImages(5);
    if($images === false) {
        echo 'Flickr Feed Unavailable';
    }
    else {
        foreach($images->photos->photo as $photo) {
            echo '<a href="http://flickr.com/photos/' . $flickr->username . '/' . $photo->attributes()->id . '"><img src="http://farm' . $photo->attributes()->farm . '.static.flickr.com/' . $photo->attributes()->server . '/' . $photo->attributes()->id . '_' . $photo->attributes()->secret . '_s.jpg" /></a> ';
        }
    }
    ?>

  5. Save your files and then take it for a test drive!

Comment

All Fields Required