After a long while of no work on my website I have decided to add a new page. Photography got a big hobby in the last 3 years and it became more important for me since I am not linving in my home country anymore. I got lots of positive feedback about my pictures and this encouraged me to create this special page on my website. Just click on the link in the menu on the left to navigate to my Flickr Bests and you get an overview about my best shots.
To retrieve my images from Flickr I use their impressive API in combination with the Zend Framework Flickr Service.
I have noticed one thing with the API it is unusual slow. I mean you would expect a much faster response than 7 - 10 seconds for just 10 pictures of your stream ? So there was the need to include caching to this site to speed up this site dramaticaly. I used the core cache class from the Zend Framework to achieve that goal, and guess what .. it works like a charm. The Zend_Service_Flickr Class provides a quite good starting point for the work with the Flickr API but its can just return images. To support list, like a list of all sets you need to provide additional methods to create a new resultset for your view.
That is not very difficult and could be done by extending a new class called My_Service_Flickr. I dont think I have to explain the naming conventions for the Zend Framework. Adding a new method like photoSet. A possible class could look like that this peace of code.
class Teejay_Service_Flickr extends Zend_Service_Flickr {
public function photoSet($userName, array $options = null)
{
static $method = 'flickr.photosets.getList';
static $defaultOptions = array('per_page' => 10,
'page' => 1,
'tag_mode' => 'or',
'extras' => 'license, date_upload, date_taken, owner_name, icon_server'
);
// merge the arrays
$options = $this->_prepareOptions($method, $options, $defaultOptions);
// now search for photos
$restClient = $this->getRestClient();
$restClient->getHttpClient()->resetParameters();
$response = $restClient->restGet('/services/rest/', $options);
// check for errors
if ($response->isError()) {
require_once 'Zend/Service/Exception.php';
throw new Zend_Service_Exception('An error occurred sending request. Status code: ' .
$response->getStatus());
}
// return the xml plain text
return $response->getBody();
}
}
A very simple controller call could be something like that
public function setsAction(){
$model = new PhotographyModel();
$outputarray = $model->getCachedFlickrSetsData($this->_apiKey,$this->_userName);
$this->view->sets = $outputarray;
}
Now you can write your model code to call the photoSet method from your newly created class. I just decided to process the returned xml code with a quick solution by using the simpleXml class from PHP.
public function getCachedFlickrSetsData($apiKey,$userName,$tags = 'Fav'){
// caching options
$frontendOptions = array('lifetime' => 86400,'automatic_serialization' => true);
$backendOptions = array('cache_dir' => $this->_config->framework->tmp_dir);
// cache id of "what we want to cache"
$id = 'SetsCache';
// create a new Cache Instance
$cache = Zend_Cache::factory('Core', 'File', $frontendOptions, $backendOptions);
// check if we chached output
if (!($outputarray = $cache->load($id))) {
$data = '';
// Instanciating our new Class
$flickr = new Teejay_Service_Flickr($apiKey);
$options = array();
$options['user_id'] = $flickr->getIdByUsername($userName);
$data = $flickr->photoSet($userName,$options);
// load xml plain text // here we need a error fall back
$xml = simplexml_load_string($data);
$results = $xml->xpath('//photoset');
// build the output array
$outputarray = array();
foreach($results as $obj){
$outputarray[] = array(
'title'=>(string) $obj->title,
'photos'=> (int) $obj->attributes()->photos,
'id'=> (string) $obj->attributes()->id
);
}
// save the output in a file
$cache->save($outputarray);
}
// just return your newly created output
return $outputarray;
}
This is a very quick solution to support a list for photosets. If you want to use it much better way I recommend to create a ResultSet Class for list which can be reused for similar tasks. I will post that here when I have time. If you have any question send me a message over my contact form, cause I have disabled the comment function, as long as I work on it.
Thomas
created on 2009-05-17 18:21:39
Work in progress, Comments will be available again soon.