【问题标题】:foldergrid php client get folder infofoldergrid php客户端获取文件夹信息
【发布时间】:2015-04-29 01:17:03
【问题描述】:

我正在尝试实现一个简单的 foldergrid php 客户端。我正在使用 foldergrid api 文档中的简单 php 客户端。我从api文档中得到的客户端代码是:

class FolderGridClient {
     protected $_cookieFileLocation = './cookie.txt';
     public $_webpage;
     public $_status;
     public $_location;

    const SERVER_HOST = 'https://secure.foldergrid.com';

    public function authenticate($username,$password, $domain){
        $params = array("domain"=>$domain,"username"=>$username,"password"=>$password);
        $this->createCurl(self::SERVER_HOST."/login",$params);
    }
    public function createDomain($adminemail,$adminpassword, $domainname, $adminfname, $adminlname){
        $json = json_encode( array('invite'=>true, 'admin' => array('email'=>$adminemail,'password'=>$adminpassword,'firstname'=>$adminfname,'lastname'=>$adminlname) ) );
        $this->createCurl(self::SERVER_HOST."/domain/".$domainname,$json,true);
    }
    public function uploadFile($file,$name,$parentDuid) {
        $fh = fopen($file, "rb");
        if($fh) {
            $json = json_encode( array('parentDuid'=>$parentDuid, 'name' => $name, 'parts' => 1) );
            $this->createCurl(self::SERVER_HOST."/file/provision",$json,true);

            if($this->_location){
                $headers = array(
                        'fg-eap: false',
                        'fg-md5:'.md5_file($file),
                        'Content-Type: binary/octet-stream'
                );

                $curl = curl_init();

                curl_setopt($curl,CURLOPT_PUT,true);
                curl_setopt($curl, CURLOPT_HEADER, TRUE);
                curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
                curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10);
                curl_setopt($curl, CURLOPT_URL, $this->_location);
                curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
                curl_setopt($curl, CURLOPT_BINARYTRANSFER, true);
                curl_setopt($curl, CURLOPT_INFILE, $fh);
                curl_setopt($curl, CURLOPT_INFILESIZE, filesize($file));
                curl_setopt( $curl, CURLOPT_VERBOSE, true );

                $this->_webpage = curl_exec($curl);
                $this->_status = curl_getinfo($curl,CURLINFO_HTTP_CODE);
                fclose($fh);
                curl_close($curl);
            }
        }else{
            echo "File not found: $file \n";
        }
    }
    public function fetchFolder($pathOrDuid){
        $this->createCurl(self::SERVER_HOST."/folder/$pathOrDuid");
        return $this->_webpage;
    }
    public function fetchFile($fileid){
        $this->createCurl(self::SERVER_HOST."/file/$fileid");
        return $this->_webpage;
    }

    public function createCurl($url,$postFields = null, $put = false)
     {
         $s = curl_init();

         curl_setopt($s, CURLOPT_VERBOSE, true);
         curl_setopt($s,CURLOPT_URL,$url);
         curl_setopt($s,CURLOPT_RETURNTRANSFER,true);
         curl_setopt($s, CURLOPT_HEADER, TRUE);
         curl_setopt($s,CURLOPT_FOLLOWLOCATION,false);
         curl_setopt($s,CURLOPT_COOKIEJAR,$this->_cookieFileLocation);
         curl_setopt($s,CURLOPT_COOKIEFILE,$this->_cookieFileLocation);

         if($postFields)
         {
             curl_setopt($s,CURLOPT_POST,true);
             if($put) {
                curl_setopt($s, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: ' . strlen($postFields)));
                curl_setopt($s,CURLOPT_CUSTOMREQUEST, "PUT");
             }else{
                curl_setopt($s, CURLOPT_HTTPHEADER, array('Expect:'));
             }
             curl_setopt($s,CURLOPT_POSTFIELDS,$postFields);
         }
         $this->_webpage = curl_exec($s);
         $this->_status = curl_getinfo($s,CURLINFO_HTTP_CODE);
         preg_match_all('/^Location:(.*)$/mi', $this->_webpage, $matches);
         $this->_location = !empty($matches[1]) ? trim($matches[1][0]) : null;
         curl_close($s);
     }

}

使用此客户端,我能够成功地将文件上传到我的文件夹网格根文件夹。现在我已经在根目录下创建了一个测试文件夹,我正在尝试使用 API 来获取该文件夹的 duid。我使用了以下代码:

$uname='myuname';
$pwd='mypwd';
$domain='mydomain';
$rootDUID='duidOFRootFolder';

$client = new FolderGridClient;
$client->authenticate( $uname,$pwd,$domain);
if($client->_status < 400){
  echo "fetchFolder: " . $client->fetchFolder($domain.'/TestNewFolder') . "<BR>";
}

TestNewFolder 是我使用 https://mydomain.foldergrid.com/show/*.html 的 foldergrid javascript 参考客户端创建的文件夹的名称

根文件夹名称与我的域名相同,我认为这是 foldergrid 的标准。

但是,当我运行该代码时,我总是得到一个未找到文件夹的响应。我尝试了文件夹路径的各种不同排列作为 fetchFolder 函数的输入,但没有成功。所以两个问题: 1.我应该如何使用php简单客户端中的fetchFolder函数来获取文件夹信息-特别是duid。 2.一旦我发现了目标文件夹的duid,是否有人在php简单客户端中添加了一个创建子文件夹的方法?

谢谢

【问题讨论】:

    标签: php grid client directory


    【解决方案1】:

    当您create a new folder programmatically using the FolderGrid API 时,您分配唯一标识该文件夹的不可变 DUID。确定任何现有文件夹的 DUID 的一种简单方法是打开 FolderGrid Web 应用程序并右键单击该文件夹以选择“显示信息”。

    要使用 API 调用以编程方式确定未知 DUID,您只需使用调用 Show Folder Details 从具有已知 DUID 的更高文件夹遍历文件夹路径,该调用会显示所有子文件夹的 DUID。例如,您将在根文件夹上执行 GET /folder/DUID,这将返回(除其他外)根文件夹的所有子文件夹的名称和 DUID 的 json 数组 - 包括 TestNewFolder

    Show Folder Details 的 API 文档中提到了一个有用的快捷方式,即您可以在该调用中用星号 '*' 代替 DUID 作为对根文件夹的速记引用。

    示例 PHP 客户端代码使用名称“pathOrDuid”作为 fetchFolder 的参数,但这是一种误导,因为调用检索 folder's details by path is a distinct call as documented here

    【讨论】:

      【解决方案2】:

      因此,按照 Simmerman 的建议,如果我知道父文件夹 duid(如根文件夹的“*”),这里有一些我用来获取子文件夹 duid 的 php 代码。我已将以下函数添加到 foldergrid 在其文档中提供的 php 简单客户端:

      public function getSubFolderDuid($parentDuid,$folderName) {
        $fetchParent = $this->fetchFolder($parentDuid);
      
        $parentJsonMatch = array();
        preg_match("/\{.*\}/",$fetchParent,&$parentJsonMatch);
        $returnArray=true;
        $parentJsonArray = json_decode($parentJsonMatch[0],$returnArray);
        $subFolders = $parentJsonArray['folders'];
      
        foreach($subFolders as $folderData) {
          if ($folderData['name'] == $folderName) return $folderData['duid'];
        }
      
        return '';
      
      }
      

      显然有足够的空间来为这个函数添加错误处理,以防父文件夹没有返回。此函数假设父文件夹存在,并以 json 数据返回。

      这是我用来调用函数的代码:

          if($client->_status < 400){
            $parentDuid = '*';
            $folderName = 'TestNewFolder';
            $folderDuid = $client->getSubFolderDuid($parentDuid,$folderName);
      
            echo "$folderName:  $folderDuid<br>";
          }
      

      有了这些构建块,我现在可以构建代码来遍历目录树或在树的任意深度找到特定文件夹 duid。

      感谢 Simmerman 为我指明正确的方向

      【讨论】:

        猜你喜欢
        • 2015-02-21
        • 2014-10-04
        • 2018-12-21
        • 1970-01-01
        • 1970-01-01
        • 2013-04-03
        • 2012-11-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多