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
- Ajax requests using jQuery and PHP
- First steps within the Zend Framework
- Create custom backups from your website using cURL
Did you enjoy this post? Why not leave a comment and continue the conversation.
Comments
Trackback URL for this post: http://www.web-development-blog.com/archives/search-for-photos-using-php-and-the-flickr-api/trackback/
Replacing the echo line in the php script with:
echo '<a href="http://www.flickr.com/photos/' . $photo["owner"] . '/' . $photo["id"] . '/" target="_blank" rel="nofollow"><img border="0" src="http://farm' . $photo["farm"] . '.static.flickr.com/' . $photo["server"] . '/' . $photo["id"] . '_' . $photo["secret"] . '.jpg" width="150" /></a>';
will show a smaller image and a link to the flickr page with all the details.
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”
This is wonderful and easy to understand tutorial. I will give it a try and use it in my coding and website to make a photo gallery.
Thanks! I’ve never played much with the API but the getSize method works nicely. The source and url attributes are helpful too!
Ajax requests using jQuery and PHP…
Our last tutorial about the flickr API was an example on how-to search the flickr photo database for images using some short PHP code. This time we will use parts from the past tutorial together with some jQuery Ajax requests to show our photo search r…
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
yes i used the same code as the tutorial but i included a tags parameter with the url:
public function search($params = null)
{
$search = ‘http://flickr.com/services/rest/?’;
if(empty($params)) $params = array();
$params['api_key'] = ‘7e5e91597a596ae828dbfd7fe52e90a5′;
$params['method'] = ‘flickr.photos.search’;
$params['format'] = ‘php_serial’;
if(empty($params)) $params = array();
foreach($params as $var => $val){
$var = urlencode($var);
$val = urlencode($val);
$search .= ‘&$var=$val’;
}
$result = file_get_contents($search);
AND THEN:
$Flickr = new PhotoSearch;
$data = $Flickr->search(array(‘tags’ => ‘white horses’));












[...] finalwebsites.com Compartelo No hay comentarios para esta publicación [...]