Flickr API
14 July 2008
Comments
flickr.com to wyszukiwarka grafik a zarazem coś w rodzaju galerii, do której możemy dodawać własne zdjęcia, całe albumy, skalować i operować na załadowanych grafikach. Dostępne zdjęcia można także komentować, oceniać. Zaletą flickr jest nie tylko mnogość opcji ale także API umożliwiające dostęp do zasobów galerii z poziomu własnych aplikacji. Na początek musimy zarejestrować się na flickr.com (za darmo) a następnie również za darmo stworzyć klucz api - link. Na stronie Flickr API znajdziemy opis całego API oraz listę dostępnych skryptów dla danego języka. W tym artykule nie będziemy korzystać z żadnego pośrednika.Z serwisem możemy porozumiewać się trzema sposobami:
- REST
- XML-RPC
- SOAP
<?php
require_once "HTTP/Request.php";
$req = new HTTP_Request('http://www.flickr.com/services/rest/?method=flickr.photos.search&text=gentoo&api_key=KLUCZ_API');
if (PEAR::isError($req->sendRequest()))
{
echo 'Nie udało się wczytać strony ';
}
$file = $req->getResponseBody();
highlight_string($file);
if (!$file = @file_get_contents('http://www.flickr.com/services/rest/?method=flickr.photos.search&text=gentoo&api_key=KLUCZ_API'))
{
echo 'Nie udało się wczytać strony ';
}
highlight_string($file);
<?xml version="1.0" encoding="utf-8" ?>
<rsp stat="ok">
<photos page="1" pages="10" perpage="100" total="978">
<photo id="146257639" owner="13999822@N00" secret="22964223f6" server="48" title="Nesting Gentoo Penguins" ispublic="1" isfriend="0" isfamily="0" />
<photo id="146250996" owner="13999822@N00" secret="fa84ba9bf5" server="38" title="Swimming Gentoo" ispublic="1" isfriend="0" isfamily="0" />
<photo id="146239765" owner="13999822@N00" secret="b795fa0288" server="46" title="Swimming Gentoo" ispublic="1" isfriend="0" isfamily="0" />
<photo id="146238256" owner="13999822@N00" secret="71aa0947e0" server="47" title="Swimming Penguins (King & Gentoo)" ispublic="1" isfriend="0" isfamily="0" />
<photo id="145869311" owner="78807897@N00" secret="983a855064" server="51" title="Screenshot Linux Gentoo Kde" ispublic="1" isfriend="0" isfamily="0" />
<photo id="145794300" owner="35143669@N00" secret="77349e9ef9" server="49" title="Screenshot! @ 14.05.06 00:27" ispublic="1" isfriend="0" isfamily="0" />
<photo id="145626529" owner="97717190@N00" secret="d7844c0554" server="47" title="One Gentoo One Rockhopper" ispublic="1" isfriend="0" isfamily="0" />
<photo id="145566125" owner="95648763@N00" secret="b31c6867ca" server="51" title="Mess" ispublic="1" isfriend="0" isfamily="0" />
<photo id="145474701" owner="13083950@N00" secret="d0e3893f83" server="50" title="flux" ispublic="1" isfriend="0" isfamily="0" />
<photo id="145456698" owner="80481047@N00" secret="dc38ed4631" server="52" title="_DSC4701" ispublic="1" isfriend="0" isfamily="0" />
<photo id="145456699" owner="80481047@N00" secret="d43ecb6f80" server="51" title="_DSC4702" ispublic="1" isfriend="0" isfamily="0" />
<photo id="143997701" owner="45075446@N00" secret="6536dcd551" server="47" title="State of the screen, nine days later..." ispublic="1" isfriend="0" isfamily="0" />
<photo id="142824964" owner="85936780@N00" secret="9d5faadf00" server="50" title="Asahiyama zoo, Asahikawa, Japan" ispublic="1" isfriend="0" isfamily="0" />
<photo id="142326029" owner="55075733@N00" secret="85c8bdd5bc" server="54" title="'Time for dinner' Gentoo Penguins, Godthul" ispublic="1" isfriend="0" isfamily="0" />
<photo id="142325283" owner="55075733@N00" secret="f6667e788f" server="48" title="Gentoo Penguin family, Prion Island" ispublic="1" isfriend="0" isfamily="0" />
<photo id="142325206" owner="55075733@N00" secret="84564b1782" server="50" title="Gentoo's and Fur Seals, Prion Island" ispublic="1" isfriend="0" isfamily="0" />
</photos>
</rsp>
Oto prosta klasa zwracająca tablicę z danymi dla kilku metod flickr API. Nazwy odpowiadają tym z Flickra:
<?php
class flickr
{
public function __construct($api_key)
{
$this->api_key = $api_key;
}
public function search($query)
{
$x = simplexml_load_file('http://www.flickr.com/services/rest/?method=flickr.photos.search&text='.$query.'&api_key='.$this->api_key);
$ret['total'] = (int)$x->photos['total'];
foreach($x->photos->photo as $res)
{
$r[] = array('id' => (int)$res['id'], 'title' => (string)$res['title'], 'owner' => (string)$res['owner'], 'secret' => (string)$res['secret']);
}
$ret['img'] = $r;
return $ret;
}
public function getContext($photo_id)
{
$x = simplexml_load_file('http://www.flickr.com/services/rest/?method=flickr.photos.getContext&photo_id='.$photo_id.'&api_key='.$this->api_key);
$ret = array('prev' => array('id' => (int)$x->prevphoto['id'], 'title' => (string)$x->prevphoto['title'], 'url' => (string)$x->prevphoto['url'], 'thumb' => (string)$x->prevphoto['thumb']), 'next' => array('id' => (int)$x->nextphoto['id'], 'title' => (string)$x->nextphoto['title'], 'url' => (string)$x->nextphoto['url'], 'thumb' => (string)$x->nextphoto['thumb']));
return $ret;
}
public function getSizes($photo_id)
{
$x = simplexml_load_file('http://www.flickr.com/services/rest/?method=flickr.photos.getSizes&photo_id='.$photo_id.'&api_key='.$this->api_key);
foreach($x->sizes->size as $res)
{
$ret[] = array('width' => (int)$res['width'], 'height' => (int)$res['height'], 'source' => (string)$res['source'], 'url' => (string)$res['url']);
}
return $ret;
}
public function getInfo($photo_id)
{
$x = simplexml_load_file('http://www.flickr.com/services/rest/?method=flickr.photos.getInfo&photo_id='.$photo_id.'&api_key='.$this->api_key);
$ret = array('secret' => (string)$x->photo['secret'], 'dateuploaded' => (int)$x->photo['dateuploaded'], 'url' => (string)$x->photo->urls->url,
'owner' => array('nsid' => (int)$x->photo->owner['nsid'], 'username' => (string)$x->photo->owner['username'], 'realname' => (string)$x->photo->owner['realname'], 'location' => (string)$x->photo->owner['location']));
return $ret;
}
public function getRecent($per_page)
{
$x = simplexml_load_file('http://www.flickr.com/services/rest/?method=flickr.photos.getRecent&per_page='.$per_page.'&api_key='.$this->api_key);
foreach($x->photos->photo as $res)
{
$ret[] = array('id' => (int)$res['id'], 'title' => (string)$res['title'], 'owner' => (string)$res['owner'], 'secret' => (string)$res['secret']);
}
return $ret;
}
// przykładowe użycie
$a = new flickr('KLUCZ');
print_r($a->getRecent(10));
}
Array ( [0] => Array ( [id] => 148203694 [title] => BPS [owner] => 32209495@N00 [secret] => 4194ba4391 ) [1] => Array ( [id] => 148203690 [title] => alfred sleeping [owner] => 15821971@N00 [secret] => 87f2a90074 ) [2] => Array ( [id] => 148203660 [title] => Over the Shoulder [owner] => 13404703@N00 [secret] => cf5d30eed2 ) [3] => Array ( [id] => 148203687 [title] => Simple [owner] => 56427717@N00 [secret] => 07255e0692 ) [4] => Array ( [id] => 148203686 [title] => GetThumb;jsessionid=B794DDE3ACDBF37BF7CECFADF7C688E1 [owner] => 82997612@N00 [secret] => a902df5230 ) )
RkBlog
Comment article