Search for photos using PHP and the flickr API
This PHP tutorial will show how to create a simple PHP class to search the flickr site for some photos. To get the data we use the flickr API to run a simple search and return the results in a serialized array.
We will be using the php function "file_get_contents" to receive data from flickr. The data which we will receive will be a serialized PHP array which means all we need to do is unserialize the array and we will easily be able to use the data returned. As an alternative we can use a cURL function to get the data, for example if the function "file_get_contents" is not allowed on your web host.
API Authentication
The API methods we use in this tutorial doesn’t require any authentication, but we need to use an API key. Get your API key on the flickr web application site (You need an Yahoo account to login).
Provide some information about your application:
and obtain your personal flickr API key and secret (We need only the key in this tutorial)
Flickr PHP Class
First we create the class structure for our class, we define a private variable for the "API key", a class constructor and the function that will search later for the images.
class Flickr { private $apiKey = 'YOUR API KEY HERE'; public function __construct() { } public function search($query = null) { } }
The API method we will be using is "flickr.photos.search", which has a number of possible parameters. We are able to search by tags, full text, tell the API which user to search for, the minimum date it a photo was uploaded and many more. You can find a list of the parameters in the flickr documentation.
So we’re going to run a full text search and limit the number of results to 50.
public function search($query = null) { $search = 'http://flickr.com/services/rest/?method=flickr.photos.search&api_key=' . $this->apiKey . '&text=' . urlencode($query) . '&per_page=50&format=php_serial'; $result = file_get_contents($search); $result = unserialize($result); }
Notice at the end of $search we have "format=php_serial" other available response formats are rest, xml-rpc, soap and json.
The data which will be stored in the variable $result is a serialized array, so simply unserialize it and return the data. Find below the complete class code and store the code in a file named "flickr.php"
class Flickr { private $apiKey = 'YOUR API KEY HERE'; public function __construct() { } public function search($query = null) { $search = 'http://flickr.com/services/rest/?method=flickr.photos.search&api_key=' . $this->apiKey . '&text=' . urlencode($query) . '&per_page=50&format=php_serial'; $result = file_get_contents($search); $result = unserialize($result); return $result; } }
How-to use our PHP class
Create a new PHP script file and add this code:
require_once('flickr.php'); $Flickr = new Flickr; $data = $Flickr->search('design inspiration'); foreach($data['photos']['photo'] as $photo) { // the image URL becomes somthing like // http://farm{farm-id}.static.flickr.com/{server-id}/{id}_{secret}.jpg echo '<img src="http://farm' . $photo["farm"] . '.static.flickr.com/' . $photo["server"] . '/' . $photo["id"] . '_' . $photo["secret"] . '.jpg">'; }
This file simply includes the flickr class we wrote, creates a new instance of the class, runs a search for "design inspiration" and finally loops through the returned data and sends the output to the browser.
Related Posts
-
Top your sales with Google Commerce SearchAs a web shop owner you like to make your shop as popular and...










Replacing the echo line in the php script with:
will show a smaller image and a link to the flickr page with all the details.
Great post, Flickr has every photo you could ever need
Hello Keith,
thanks for your suggestion, this tutorial is a small example on what is possible using the flickr API.
Posting code in commnents is often difficult for WordPress, posting PHP code is possible within the “code” tag, but html needs a special “treatment”
Keith,
just used the tutorial for something new, maybe you should check this page instead of using the image width/height attributes
Thanks! I’ve never played much with the API but the getSize method works nicely. The source and url attributes are helpful too!
Thanks! I used this as part of the search service for nowsup.com
This is great, thanks for the info… and the beauty of it is SIMPLICITY.
I’ll be creating a few context sensitive photo galleries now
great tutorial but i’m getting the 112: Method “xxx” not found
error message when clearly the flickr.photos.search method exists. am i missing something obvious?
can anyone help?
thanks
You used exact the same code as from the tutoial?
yes i used the same code as the tutorial but i included a tags parameter with the url:
AND THEN:
The code looks different from mine
but should work.
Debug your URL first with:
echo $search;
before you use the file function (paste the URL into your browser)
Incredible, I copied the code and it shows me pictures … so everything is ok, but the problem is that it is not mine.
Yet I entered my API KEY, I do not understand …
I did a “echo $photo['secret'];” and the secret is random.
Please can you help me?
PS: forgive my English.
Hello Razhen,
the tutorial is about how-to search in the complete database.
If you like to search only your own photo’s you need to add some value for the “user_id” (check the manual link above)
Hello,
Thank you for this great tutorial.
Actually for getting the small and medium thumbnails of specific images just append “_s” or “_m”
before adding the “.jpg”
http://farm7.static.flickr.com/6212/6260200673_da9c826d9d_s.jpg
Thanks for the tip Gerald!
Hello,
Thank you for this great tutorial.
i want to ask you how can i get one image at a time (a diaporama)
thx
What is a diaporama? A kind of slideshow?
yes
That was a short answer
What you need is more than the code from this tutorial.
My advice is to search some jquery slide show plugin/script first. Try different scripts and if you think that some script works for your with static images, you need to use the code from this tutorial to replace the code for your static images.
It sounds complicated but If you know how to tweak a slide show is handling this code very easy (maybe I should write a tutorial for this)
i try this tutorial with an other code and it works but when i try it with yours it doesn’t work as i want it give me a liste of images
her is the tutorial:
http://www.webdesignbooth.com/create-a-vertical-scrolling-news-ticker-with-jquery-and-jcarousel-lite/
if you can try to make it work
thank you and sorry for my bad english
There are much better scripts and plugins, search for “jquery slide show”.
This one looks good to me: http://slidesjs.com/
Inside the example you need to replace the static content with the dynamic flickr content