Ejemplo n.º 1
0
 /**
  * 
  * Get photo set
  *
  * @param string $photoset_id  The photoset ID to chose random photo from.
  * @param string $per_page  The number of photos to display per page
  *
  * @return object
  */
 public function fetchPhotoset($id, $per_page = null)
 {
     static $method = 'flickr.photosets.getPhotos';
     if (empty($id)) {
         // @see Zend_Service_Exception
         require_once 'Zend/Service/Exception.php';
         throw new Zend_Service_Exception('You must supply a photo set ID');
     }
     // Get page number from the url - if there isn't one - we're on page 1
     $cws_page = isset($_GET['cws_page']) ? $_GET['cws_page'] : 1;
     $options = array('api_key' => $this->consumer_key, 'method' => $method, 'photoset_id' => $id, 'extras' => 'license, date_upload, date_taken, owner_name, icon_server, original_format, last_update, geo, tags, machine_tags, o_dims, views, media, path_alias, url_sq, url_t, url_s, url_m, url_o', 'per_page' => "{$per_page}", 'page' => $cws_page, 'media' => 'photo');
     // Add some caching...
     try {
         // Setup Zend Cache for 24hrs...
         // TODO: make cache duration user configurable...
         $frontendOptions = array('lifetime' => 86400, 'automatic_serialization' => true);
         $backendOptions = array('cache_dir' => WPFLICKR_PLUGIN_PATH . 'cache/');
         $cache = Zend_Cache::factory('Core', 'File', $frontendOptions, $backendOptions);
         // If we don't have cached version, grab em from Flickr
         if (($response = $cache->load('photo_set_' . $id . '_' . $options['page'] . '_' . $options['per_page'])) === false) {
             if ($this->debug) {
                 error_log('Inside: CWS_FlickrApi::get_photo_set() - This one is from Flickr servers.');
             }
             $options['privacy_filter'] = '1';
             // Now search for photos
             $consumer = new Zend_Oauth_Consumer();
             $client = $consumer->getHttpClient();
             $client->setUri("http://api.flickr.com/services/rest/");
             $client->setMethod(Zend_Http_Client::GET);
             $client->setConfig(array('timeout' => 30));
             // TODO: check this stopped the time out issue
             $client->setParameterGet($options);
             $response = $client->request();
             // Uses Zend_Config_Xml to parse xml to array
             require_once 'Zend/Config/Xml.php';
             $photoset = new Zend_Config_Xml($response->getBody());
             $cache->save($photoset, 'photo_set_' . $id . '_' . $options['page'] . '_' . $options['per_page']);
             return $photoset;
             if ($response->isError()) {
                 // @see Zend_Service_Exception
                 require_once 'Zend/Service/Exception.php';
                 throw new Zend_Service_Exception('An error occurred sending request. Status code: ' . $response > getStatus());
             }
         } else {
             if ($this->debug) {
                 error_log('Inside: CWS_FlickrApi::get_photo_set() - This one is from cache.');
             }
             $cache->save($response, 'photo_set_' . $id . '_' . $options['page']);
             return $response;
         }
     } catch (Zend_Gdata_App_Exception $ex) {
         $this->errors[] = $ex->getMessage();
         $this->get_errors($this->errors);
     }
 }