how to display your flickr photos using php

A Division of Me by toxictwo on deviantART

In order to complete this tutorial you will need to apply for a flickr API key. You will also need to download a copy of phpFlickr API Kit. The use of an API Kit makes the use of flickr’s API methods easier. You can also view the demo page of the tutorial (opens in a new tab).

Let’s begin. First, we need to include the phpFlickr main file and then create a new phpFlickr object which is stored in the $phpFlickrObj variable. Make sure you enter the correct path (where you extracted the phpFlickr files) and also enter your flickr API key.

The next step is to obtain your NSID by using your flickr username (in the example I’m using my flickr username), which is stored in the $user variable. Using this NSID, we get the url to the specific user’s photos and we store it in the $user_url variable. Finally, we get an array ($photos) containing all the relevant information (id, title, etc.) of the last four (if you want to change the number of photos, just change the number) photos appearing in the user’s photostream.

In this example we use only the id and the title of each photo. If you want to view all the information contained in the array, you can use print_r($photos).

The final step to display the photos, is to convert the multidimensional array into an array of one dimension and use a loop to display everything. The following code, displays the photos. The photos can be displayed in six different sizes. To change the size, you need to alter the “square” value. The available sizes are: square, thumbnail, small, medium, large and original.

The complete code is diplayed below. You can also view the demo page of the tutorial (opens in a new tab).

Photo by ~toxictwo

  • RedPrince

    Hi, thanks for the tutorial, got me up and running in no time.

    I’ve managed to display the photo’s title with it but struggling with displaying the photo’s description. Could you advise on how to display this please. It’s not one of the values in the $photos array.

    Many thanks,
    Dave

  • Stathis

    Hello Dave,

    You can get the photo’s description using the following code (place it into the foreach loop of the example):

    $info = $f->photos_getInfo($photo[id]);
    echo $info[‘description’];

    I hope that helps.

  • http://wahome.orgfree.com Samuel

    Thank you I got your code and it worked fine.
    But can you explain how you display the next set of results from a search maybe?
    Thanks

  • Stathis

    Hello Samuel,

    Can you explain in more detail your question? I cannot understand what you want me to explain :-)

  • rathin

    Hai, really nice demo. but when I give my yahooid.it’s shown error but if i give my flickr screen name it’s give me the reply.can just tell me how can i make it using yahoo id.

  • Stathis

    @rathin:
    If you mean the id that Flickr has generated for you (NSID) do the following: remove the “$user = $phpFlickrObj->people_findByUsername($username);” line and then hard-code your id in the following two lines in the place of “$user[‘id’]”.

    If you mean your e-mail, then instead of “$user = $phpFlickrObj->people_findByUsername($username);” use “$user = $phpFlickrObj->people_findByEmail($email);”.

    I hope that helps. You can find all the available Flickr’s API functions here: http://www.flickr.com/services/api/

  • niraj

    hey nice blog, i just want to ask,
    i added the code, and wordks fine, but if i am using different usernames then i am getting errors
    for eg,
    if i change the username to “vlerick” it gives me this error
    “Warning: Invalid argument supplied for foreach() in \page.php on line 11”

    same thing when i tried on your demo, it gives me the same error
    the flickr url of this is : http://www.flickr.com/people/vlerick/

    whats the problem?

  • Muhammad Fahad Nasir

    Salam,
    Nice work buddy, this blog really helped me. Thanks

  • Ian Sampaio

    I’m building a multi authors blog and I need to use this beautiful piece of code in wordpress author’s page (author.php) getting user name from a custom field from admin.

    I’m struggling to get this working. All I get so far is the account name printed and nothing else:

    “$var = the_author_meta( ‘flickr’ );

    $user = $phpFlickrObj->people_findByUsername($var);
    $user_url = $phpFlickrObj->urls_getUserPhotos($user[‘id’]);
    $photos = $phpFlickrObj->people_getPublicPhotos($user[‘id’], NULL, NULL, 4);”

    Can you give a direction?

    Thanks!

  • http://www.intiaz.com Intiaz

    Hello,
    I’ve successfully installed your code on my “Contacts Page” but I would like to show one of Flickr “Sets” instead of just my global photo feed. Is this possible?

    Please get back to me at your earliest convenience.
    Much appreciated!
    Intiaz

  • Satenik

    Hi nice job thanks, but can you please hekp .. how I can make a search with keyword (like for example “cat dog”) how I have to change the followomg code

    $user = $phpFlickrObj->people_findByUsername($var);
    $user_url = $phpFlickrObj->urls_getUserPhotos($user[‘id’]);
    $photos = $phpFlickrObj->people_getPublicPhotos($user[‘id’], NULL, NULL, 4);”

    for getting only ctas and dogs images

    thanks in advanced.

  • Sangram keshari patra

    Hi,Nice stuff.

    Is there any way to upload photo in my site and it will saved into my flicker account.Means I want to show a button like upload photo here and these photo will also added to your flicker account.So any ideas..

    Cheers!!!!

  • Stathis

    @Sangram: I believe you can do it using Flickr’s API. Check the following URL for more info: http://www.flickr.com/services/api/upload.api.html

  • http://facebook.com/ordondo Andaeii

    very 9ice blog… make sense… love this… i’m tinking.. how do i get the user ID using the api.. is there no direct way to obtain a user id… and also how can i request a larger photo…

    9ice tutorial by the way, very helpful…

    @Andaeii on Twitter

  • Paul

    Great script and got it up running in no time. But I’ve been trying to filter the results by tags, or a specific tag. So if I use the above code and want to filter by “barcelona” to only show these:
    http://www.flickr.com/photos/stathisg/tags/barcelona/

    What would I need to change? Been trying for hours with no joy.

    Thanks!

  • Kevin

    NICE

  • Kevin

    How Can I View Image According to their keyword? Please Help

  • Dhrmesh Master

    Invalid argument supplied for foreach()

  • 3D Rendering Services

    Really very helpful to me.

  • imran

    hello sir.
    the code works great if we want to search by user name.
    i am building an application which will display homogeneous images.
    i want to retrieve all the images of flicker.
    Will you please tell me how can i retrieve all the images.

    • Stathis

      Hello imran,

      Check out the API of Flickr to see if there is function that covers your needs.

  • maksudkhan

    Invalid argument supplied for foreach()

    • http://burnmind.com/ StathisG

      Thanks for letting me know. The tutorial is quite old (written almost 5 years ago), so probably something changed in Flickr’s API, causing it to break. I’ll have a look as soon as I find the time, and I’ll update the tutorial.