d
Amit DhamuSoftware Engineer
 

Flickr Photo Search API

3 minute read 00000 views

In order to use the Flickr API, you will need a Yahoo account to sign in and apply for an API key which is generated once you log in. You can do that here.

$f_api_key = "YOUR_API_KEY";
$f_secret = "YOUR_SECRET";

$f_method = "flickr.photos.search";
$f_limit = "10";
$f_search = urlencode("YOUR SEARCH PARAMETER");

$f_url = "http://flickr.com/services/rest/?method=$f_method&api_key=$f_api_key&text=$f_search&per_page=$f_limit";
$f_get = file_get_contents($f_url);
$f_result = new SimpleXMLElement($f_get);

$photos = $f_result->photos;

for ($i = 0; $i < $f_limit; $i++) {
    $id = $photos->photo[$i]["id"];
    $owner = $photos->photo[$i]["owner"];
    $secret = $photos->photo[$i]["secret"];
    $server = $photos->photo[$i]["server"];
    $farm = $photos->photo[$i]["farm"];
    $title = $photos->photo[$i]["title"];
    $isPublic = $photos->photo[$i]["ispublic"];
    $isFriend = $photos->photo[$i]["isfriend"];
    $isFamily = $photos->photo[$i]["isfamily"];
    $image = "http://farm" . $farm . ".static.flickr.com/" . $server . "/" . $id . "_" . $secret . ".jpg";
}

A typical XML response looks like:

<rsp stat="ok">
    <photos page="1" pages="37513" perpage="10" total="375130">
        <photo id="6955685327" owner="77546411@N07" secret="cccac6dc65" server="7070" farm="8" title="??.JPG" ispublic="1" isfriend="0" isfamily="0"/>
        <photo id="6955642749" owner="71518188@N00" secret="cc0f39070f" server="7193" farm="8" title="Subway" ispublic="1" isfriend="0" isfamily="0"/>
        <photo id="6809499654" owner="34544248@N04" secret="273de15148" server="7185" farm="8" title="Your Move" ispublic="1" isfriend="0" isfamily="0"/>
        <photo id="6809507648" owner="57380939@N02" secret="ca30a99202" server="7195" farm="8" title="Reeder for iPad" ispublic="1" isfriend="0" isfamily="0"/>
        <photo id="6955615627" owner="76109530@N08" secret="97b86e05db" server="7192" farm="8" title="Caspost.com-stunninglegolifesize-ipad copy" ispublic="1" isfriend="0" isfamily="0"/>
        <photo id="6955590793" owner="52270029@N02" secret="2793d7241e" server="7177" farm="8" title="Music Of Time" ispublic="1" isfriend="0" isfamily="0"/>
        <photo id="6955582241" owner="73214385@N03" secret="34b2d9ba32" server="7039" farm="8" title="day sixty-four." ispublic="1" isfriend="0" isfamily="0"/>
        <photo id="6809483686" owner="52270029@N02" secret="a05f155269" server="7208" farm="8" title="Beauty Of Glass" ispublic="1" isfriend="0" isfamily="0"/>
        <photo id="6809481434" owner="52270029@N02" secret="f4f9a0cf02" server="7189" farm="8" title="Time Master" ispublic="1" isfriend="0" isfamily="0"/>
        <photo id="6955592185" owner="52270029@N02" secret="2eacfde150" server="7058" farm="8" title="Music Of Time" ispublic="1" isfriend="0" isfamily="0"/>
    </photos>
</rsp>