【问题标题】:Display all image files in Box folder via PHP通过 PHP 显示 Box 文件夹中的所有图像文件
【发布时间】:2013-09-19 05:28:12
【问题描述】:

我对 Box 环境完全陌生。我正在使用当前代码在网站上的 web 目录中显示所有图像,并首先列出最新文件:

<?php
   $path = 'images/';
   $files = scandir($path);
   $ignore = array( 'cgi-bin', '.', '..');

   # remove ignored files
   $files = array_filter($files, function($file) use ($ignore) {return !in_array($file, $ignore);});

   # get the modification time for each file
   $times = array_map(function($file) use ($path) {return filemtime("$path/$file");}, $files);

   # sort the times array while sorting the files array as well
   array_multisort($times, SORT_DESC, SORT_NUMERIC, $files);

   foreach ($files as $file) {
      echo '<div class="item">';
      echo '<a title="&copy;2013" rel="gallery" class="fancybox" href="images/'.$file.'"><img   src="images/'.$file.'" alt="'.$image.'" /></a>';
      echo '</div>';
   }
?>

我想集成 Box API 以从我的 Box 文件夹而不是 Web 文件夹中获取文件。当前的 API 可以做到这一点吗?我尝试使用以下内容显示 Open Access 文件夹的内容:

<?php
   $params = array();
   $params['shared_link'] = array("access"=> "Open");
   $params = json_encode($params);
   echo $params;
   $key = "[my api key]";
   $token = "[token]";
   $ch = curl_init();

   curl_setopt($ch, CURLOPT_URL, "https://api.box.com/2.0/folders/kvpemb6rgohhr448r935"); //my box folder
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
   curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
   curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
   curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
   curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', "-H Authorization: Bearer $key",'Content-Length: ' . strlen($params), 'X-HTTP-Method-Override: GET'));
   $result = curl_exec($ch);
   curl_close($ch);
   print_r($result);
?>

但只接收页面上的数组值"{"shared_link":{"access":"Open"}}"

我已经用尽了我在 Google 和 Stackoverflow 上的搜索能力,并且没有遇到试图完成此任务的线程。感谢您的任何指导/帮助。

【问题讨论】:

    标签: php curl box-api scandir


    【解决方案1】:

    如果您想从 Box 中的“打开”文件夹中检索项目,我建议您查看此端点。 http://developers.box.com/docs/#shared-items

    【讨论】:

      【解决方案2】:

      在这里,也许这会对你有所帮助:

      function getPictures($folderid, $access_token){
          //===================== Default cUrl options =================
          $options = array(
              CURLOPT_SSL_VERIFYPEER => false,
              CURLOPT_VERBOSE        => true,
              CURLOPT_HEADER         => false,
              CURLINFO_HEADER_OUT    => false,
              CURLOPT_RETURNTRANSFER => true,
              CURLOPT_FOLLOWLOCATION => false,
          );
          $options[CURLOPT_HTTPHEADER] = array ("Authorization: Bearer ".$access_token);
      
          //======================= Proper url ==========================
          $url = "https://api.box.com/2.0/folders/{$folderid}/items";
      
          //======================= cUrl call ===========================
          $ch = curl_init($url);
          curl_setopt_array($ch, $options);
          $result = curl_exec($ch);
          curl_close($ch);
          $result = json_decode($result, true);
      
          // =============== Loop over items to search for photos =================
          $rez = array();
          if (isset($result['total_count']) && $result['total_count'] > 0){
              foreach ($result['entries'] as $elements){
                  if (isPic($elements['name'])) $rez[] = $elements['name'];
              }
          }
      
          return $rez;
      }
      
      function isPic($value){
          $value = explode('.', $value);
          if (count($value) < 2) return false;
          $extensions = array ('jpg', 'bmp', 'png', 'gif');
          return in_array($value[1], $extensions);
      }
      

      来自 Box.com api 的有用链接:

      http://developers.box.com/docs/#folders-retrieve-a-folders-items

      http://developers.box.com/docs/#search

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-02-16
        • 1970-01-01
        • 2021-02-27
        • 2011-09-11
        • 2020-06-07
        • 2014-01-18
        • 1970-01-01
        相关资源
        最近更新 更多