Flickr API 搜索?
我有一些 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";
}
?>
I have some php code that retrieves a set of images based of a photoset. I want to manipulate this code so that it grabs a set of pictures from a search term.
I know you can use the method inbuilt into the api: flickr.photos.search
Just wondering how i can apply it to this code
<?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";
}
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你读过文档吗? flickr.photos.search
这些方法返回类似的数据,但顶级数组的名称不同(照片与照片集)。假设您修改了参数以使用正确的方法并传递适当的参数,那么您唯一需要更改的是这一行:
Did you read the documentation? flickr.photos.search
These methods return similar data, but the name of the top-level array is different (photos vs. photoset). Assuming you modified your parameters to use the correct method and pass the appropriate arguments, the only other thing you need to change is this line:
使用此查询网址:
http ://flickr.com/services/rest/?method=flickr.photos.search&api_key=YOUR-KEY&text=YOUR-SEARCH TERM&per_page=10&format=php_serial
Use this query url:
http://flickr.com/services/rest/?method=flickr.photos.search&api_key=YOUR-KEY&text=YOUR-SEARCH TERM&per_page=10&format=php_serial