the internets!

~ Wednesday, April 29 ~
Permalink

PHP Flickr class


Bueno, queria agregar fotos de flickr, se que hay varias librerias de php, pearl, y demas para poder ya usar la api de flickr para obtener fotos, pero donde queda lo divertido ahi :P, asi que me puse hacer una pequeña clase para obtener las fotos de mi cuenta flickr a travez de la api rest y feeds, hubiera usado por feed, pero obtener fotos de un set no vi informacion en la documentacion sobre esto, solo vi el metodo via rest request.


Para usarla, ocupas una api key, mas que nada para obtener los sets de dicha cuenta, para obtener las fotos recientes y las fotos contenidas en un set, teniendo el id del set y nsid, con eso es suficiente :)


Aun me faltan cosas por implementar, de momento solo le puse que obtenga fotos recientes, los sets, y obtener fotos de un set determinado, talvez despues si le meta mas funcionalidad :)


Un ejemplos sencillos:


// Obtener los sets
$arrSets = Flickr::getSets();
foreach ($arrSets as $oSet) {
    echo $oSet->getUrl().' - ' .$oSet->getThumbUrl().' - ' .$oSet->getTitle();
}


//Para obtener las fotos recientes
$arrPhotos = Flickr::getRecentPhotos();
foreach ($arrPhotos as $oPhoto) {
    echo  $oPhoto->getTitle().' - '.$oPhoto->getLink(). ' - ' . $oPhoto->getSmallUrl();
}


//Obtener las fotos de un set:
$numSetId = 72157616583218093;
$arrPhotos = Flickr::getPhotosFromSet($numSetId);
foreach ($arrPhotos as $oPhoto) {
    echo  $oPhoto->getTitle().' - '.$oPhoto->getLink(). ' - ' . $oPhoto->getSmallUrl();
}

Source:

Flickr Class:


<?php
class Flickr
{
    const 
URL_PHOTOSET_FEED 'http://api.flickr.com/services/feeds/photoset.gne?set=:SETID&nsid=:NSID&format=json';
    const 
URL_RECENT_PHOTOS 'http://api.flickr.com/services/feeds/photos_public.gne?id=:NSID&lang=en-us&format=json';
    const 
URL_REQUEST_SETS  'http://api.flickr.com/services/rest/?method=flickr.photosets.getList&api_key=:APIKEY&user_id=:NSID&format=json';
    
// Id del usuario de flickr
    
const NSID              'Flickr user id';
    
// Llave de api
    
const API_KEY           'Flickr api key';
    
// Url de la cuenta.
    
const URL               'http://www.flickr.com/photos/??????';

    private static function 
_call($strUrl)
    {
        
$curl_handle curl_init($strUrl);
        
curl_setopt($curl_handle
                    
CURLOPT_RETURNTRANSFER
                    
true);
        
$response curl_exec($curl_handle);
        
curl_close($curl_handle);
        
$response str_replace("jsonFlickrApi({""{"$response); 
        
$response str_replace("jsonFlickrFeed({""{"$response);
        
$response substr_replace($response,"",-1);
        
$strJson  json_decode($response);
        return 
$strJson;
    }

    public static function 
getSets()
    {
        
$arrSets = array();
        
$strUrl  self::URL_REQUEST_SETS;
        
$strUrl  str_replace(':APIKEY'
                               
self::API_KEY
                               
$strUrl);
        
$strUrl  str_replace(':NSID'
                               
self::NSID
                               
$strUrl);
        
$jsnSets self::_call($strUrl);
        foreach (
$jsnSets->photosets->photoset as $strJson) {
            
$arrSets[] = self::buildSetObject($strJson);
        }
        return 
$arrSets;
    }

    public static function 
getPhotosFromSet($numSetId)
    {
        
$arrPhotos = array();
        
$strUrl    self::URL_PHOTOSET_FEED;
        
$strUrl    str_replace(':SETID'
                                 
$numSetId
                                 
$strUrl);
        
$strUrl    str_replace(':NSID'
                                 
self::NSID
                                 
$strUrl);
        
$jsnPhotos self::_call($strUrl);
        if (isset(
$jsnPhotos->items
            && 
is_array($jsnPhotos->items)) {
            foreach (
$jsnPhotos->items as $oStd) {
                
$arrPhotos[] = self::buildPhotoObject($oStd);
            }
        }
        return 
$arrPhotos;
    }

    public static function 
getRecentPhotos()
    {
        
$arrPhotos = array();
        
$strUrl    self::URL_RECENT_PHOTOS;
        
$strUrl    str_replace(':NSID'
                                 
self::NSID
                                 
$strUrl);
        
$jsnPhotos self::_call($strUrl);
        if (isset(
$jsnPhotos->items
            && 
is_array($jsnPhotos->items)) {
            foreach (
$jsnPhotos->items as $oStd) {
                
$arrPhotos[] = self::buildPhotoObject($oStd);
            }
        }
        return 
$arrPhotos;
    }

    private static function 
buildSetObject($oStd)
    {
        
$oSet = new FlickrSet();
        
$oSet->setId($oStd->id);
        
$oSet->setPrimary($oStd->primary);
        
$oSet->setSecret($oStd->secret);
        
$oSet->setServer($oStd->server);
        
$oSet->setFarm($oStd->farm);
        
$oSet->setPhotos($oStd->photos);
        
$oSet->setVideos($oStd->videos);
        
$oSet->setTitle($oStd->title->_content);
        
$oSet->setDescription($oStd->description->_content);
        
$oSet->setUrl(self::URL 
                      
'/sets/' 
                      
$oStd->id);
        return 
$oSet;
    }

    private static function 
buildPhotoObject($oStd)
    {
        
$oPhoto = new FlickrPhoto();
        
$oPhoto->setTitle($oStd->title);
        
$oPhoto->setLink($oStd->link);
        
$oPhoto->setSmallUrl($oStd->media->m);
        
$oPhoto->setDate($oStd->date_taken);
        
$oPhoto->setDescription($oStd->description);
        
$oPhoto->setPublished($oStd->published);
        
$oPhoto->setAuthor($oStd->author);
        
$oPhoto->setAuthorId($oStd->author_id);
        
$oPhoto->setTags($oStd->tags);
        return 
$oPhoto;
    }
}

FlickrPhoto Class


class FlickrPhoto
{
    
/** DOC */
    
private $_strTitle;
    
/** DOC */
    
private $_strLink;
    
/** DOC */
    
private $_strSmallUrl;
    
/** DOC */
    
private $_strDate;
    
/** DOC */
    
private $_strDescription;
    
/** DOC */
    
private $_strPublished;
    
/** DOC */
    
private $_strAuthor;
    
/** DOC */
    
private $_strAuthorId;
    
/** DOC */
    
private $_strTags;
    
    public function 
__construct()
    {
    }
    
/**
     * 
     * Set para ....
     * @param string $strTitle
    */
    
public function setTitle($strTitle)
    {
        
$this->_strTitle $strTitle;
    }
    
/**
     * 
     * Get para ....
     * @return string
    */
    
public function getTitle()
    {
        return 
$this->_strTitle;
    }
    
/**
     * 
     * Set para ....
     * @param string $strLink
    */
    
public function setLink($strLink)
    {
        
$this->_strLink $strLink;
    }
    
/**
     * 
     * Get para ....
     * @return string
    */
    
public function getLink()
    {
        return 
$this->_strLink;
    }
    
/**
     * 
     * Set para ....
     * @param string $strSmallUrl
    */
    
public function setSmallUrl($strSmallUrl)
    {
        
$this->_strSmallUrl $strSmallUrl;
    }
    
/**
     * 
     * Get para ....
     * @return string
    */
    
public function getSmallUrl()
    {
        return 
$this->_strSmallUrl;
    }
    
/**
     * 
     * Set para ....
     * @param string $strDate
    */
    
public function setDate($strDate)
    {
        
$this->_strDate $strDate;
    }
    
/**
     * 
     * Get para ....
     * @return string
    */
    
public function getDate()
    {
        return 
$this->_strDate;
    }
    
/**
     * 
     * Set para ....
     * @param string $strDescription
    */
    
public function setDescription($strDescription)
    {
        
$this->_strDescription $strDescription;
    }
    
/**
     * 
     * Get para ....
     * @return string
    */
    
public function getDescription()
    {
        return 
$this->_strDescription;
    }
    
/**
     * 
     * Set para ....
     * @param string $strPublished
    */
    
public function setPublished($strPublished)
    {
        
$this->_strPublished $strPublished;
    }
    
/**
     * 
     * Get para ....
     * @return string
    */
    
public function getPublished()
    {
        return 
$this->_strPublished;
    }
    
/**
     * 
     * Set para ....
     * @param string $strAuthor
    */
    
public function setAuthor($strAuthor)
    {
        
$this->_strAuthor $strAuthor;
    }
    
/**
     * 
     * Get para ....
     * @return string
    */
    
public function getAuthor()
    {
        return 
$this->_strAuthor;
    }
    
/**
     * 
     * Set para ....
     * @param string $strAuthorId
    */
    
public function setAuthorId($strAuthorId)
    {
        
$this->_strAuthorId $strAuthorId;
    }
    
/**
     * 
     * Get para ....
     * @return string
    */
    
public function getAuthorId()
    {
        return 
$this->_strAuthorId;
    }
    
/**
     * 
     * Set para ....
     * @param string $strTags
    */
    
public function setTags($strTags)
    {
        
$this->_strTags $strTags;
    }
    
/**
     * 
     * Get para ....
     * @return string
    */
    
public function getTags()
    {
        return 
$this->_strTags;
    }
}
FlickrSet.php

class FlickrSet
{
    private 
$_numId 0;
    private 
$_numPrimary 0;
    private 
$_strSecret '';
    private 
$_numServer 0;
    private 
$_numFarm   0;
    private 
$_numPhotos 0;
    private 
$_numVideos 0;
    private 
$_strTitle  '';
    private 
$_strDescription '';
    private 
$_strUrl;

    public function 
__construct()
    {
    }

    public function 
setId($numId)
    {
        
$this->_numId $numId;
    }
    public function 
getId()
    {
        return 
$this->_numId;
    }

    public function 
setPrimary($numVar)
    {
        
$this->_numPrimary $numVar;
    }
    public function 
getPrimary()
    {
        return 
$this->_numPrimary;
    }

    public function 
setSecret($strVar)
    {
        
$this->_strSecret $strVar;
    }
    public function 
getSecret()
    {
        return 
$this->_strSecret;
    }

    public function 
setServer($numVar)
    {
        
$this->_numServer $numVar;
    }
    public function 
getServer()
    {
        return 
$this->_numServer;
    }

    public function 
setFarm($numVar)
    {
        
$this->_numFarm $numVar
    }
    public function 
getFarm()
    {
        return 
$this->_numFarm;
    }

    public function 
setPhotos($numVar)
    {
        
$this->_numPhotos $numVar;
    }
    public function 
getPhotos()
    {
        return 
$this->_numPhotos;
    }

    public function 
setVideos($numVar)
    {
        
$this->_numVideos $numVar;
    }
    public function 
getVideos()
    {
        return 
$this->_numVideos;
    }

    public function 
setTitle($strVar)
    {
        
$this->_strTitle $strVar;
    }
    public function 
getTitle()
    {
        return 
$this->_strTitle;
    }

    public function 
setDescription($strDescription)
    {
        
$this->_strDescription;
    }
    public function 
getDescription()
    {
        return 
$this->_strDescription;
    }

    public function 
setUrl($strUrl)
    {
        
$this->_strUrl $strUrl;
    }

    public function 
getUrl()
    {
        return 
$this->_strUrl;
    }

    public function 
getThumbUrl()
    {
        
$strUrl  'http://farm'.$this->getFarm().'.static.flickr.com/' 
                   
$this->getServer().'/';
        
$strUrl .= $this->getPrimary().'_'.$this->getSecret().'_s.jpg';
        return 
$strUrl;
    }

}

Para bajar el codigo, desde aqui

Tags: dev php
 ()