Flickr API 搜索?

发布于 2024-11-04 10:55:27 字数 1632 浏览 0 评论 0原文

我有一些 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

画▽骨i 2024-11-11 10:55:27

你读过文档吗? flickr.photos.search

这些方法返回类似的数据,但顶级数组的名称不同(照片照片集)。假设您修改了参数以使用正确的方法并传递适当的参数,那么您唯一需要更改的是这一行:

$photos = $rsp_obj["photos"]["photo"];

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:

$photos = $rsp_obj["photos"]["photo"];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文