16 September, 2010

Search Flickr by username using REST

Searching flickr users by username can be done simply using REST services provided by flickr. Following is a beginning but complete
code that search for a username in flick using REST service of flickr.


//REST url for flickr
$url = "http://api.flickr.com/services/rest";
$data = array(
"username" => "sudhi",
"method" => "flickr.people.findByUsername",
"api_key" => "ur_api_key_here"
);
//this is the query string initialization
$q = http_build_query($data);
$finalUrl = $url."?".$q;
//initialize curl
$ch = curl_init($finalUrl);
//return the response as string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//execute the curl
$result = curl_exec($ch);
//use simpleXml for parsing the xml response
$xmlStr = simplexml_load_string($result);
foreach($xmlStr->user as $people) {
$attrs = $user->attributes();
//ths $attrs contains user information
echo "
";
print_r($attrs);
echo "
";
}

No comments: