我有一些PHP代码,可以根据照片集检索一组图像。我想操纵这段代码,以便从搜索词中抓取一组图片。
我知道你可以使用内置于api中的方法:flickr.photos.search
只是想知道如何将它应用于此代码
<?php
$params = array(
'api_key' => '2292bec0973f91d9f62fb606f85ee031',
'method' => 'flickr.photosets.getPhotos',
'photoset_id' => '72157622566216264',
'extras' => 'original_format',
'format' => 'php_serial'
);
$encoded_params = array();
foreach ($params as $k => $v){ $encoded_params[] = urlencode($k).'='.urlencode($v); }
$ch = curl_init();
$timeout = 5; // set to zero for no timeout
curl_setopt ($ch, CURLOPT_URL, 'http://api.flickr.com/services/rest/?'.implode('&', $encoded_params));
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$file_contents = curl_exec($ch);
curl_close($ch);
$rsp_obj = unserialize($file_contents);
if ($rsp_obj['stat'] == 'ok') {
$photos = $rsp_obj["photoset"]["photo"];
echo "
<ul>";
foreach($photos as $photo) {
$farm = $photo['farm'];
$server = $photo['server'];
$photo_id = $photo['id'];
$secret = $photo['secret'];
$photo_title = $photo['title'];
echo '<li><img src="http://farm'.$photo['farm'].'.static.flickr.com/'.$photo['server'].'/'.$photo['id'].'_'.$photo['secret'].'_t.jpg" alt="'.$photo['title'].'" ></li>';
}
echo "
</ul>
";
} else {
echo "Error getting photos";
}
?>
答案 0 :(得分:3)
您是否阅读过文档? flickr.photos.search
这些方法返回类似的数据,但顶级数组的名称不同(照片与 photoset )。假设您修改了参数以使用正确的方法并传递适当的参数,那么您需要更改的另一件事就是这一行:
$photos = $rsp_obj["photos"]["photo"];
答案 1 :(得分:0)
使用此查询网址:
http://flickr.com/services/rest/?method=flickr.photos.search&api_key=YOUR-KEY&text=YOUR-SEARCH TERM&amp; per_page = 10&amp; format = php_serial