foldergrid php客户端获取文件夹信息

时间:2014-02-07 20:40:08

标签: php grid client directory

我正在尝试实现一个简单的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);
     }

}

使用此客户端,我能够成功将文件上传到我的文件夹网格根文件夹。现在我在root下创建了一个测试文件夹,我正在尝试使用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/使用foldergrid javascript引用客户端创建的文件夹的名称* .html

root foldername与我的域名相同,我认为这是foldergrid的标准。

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

感谢

2 个答案:

答案 0 :(得分:0)

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

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

Show Folder Details的API文档中提到了一个有用的快捷方式,即您可以在该调用中将asterisk'*'替换为DUID,作为根文件夹的简写引用。

示例PHP客户端代码使用名称“pathOrDuid”作为fetchFolder的参数,但由于调用folder's details by path is a distinct call as documented here的调用,这会产生误导。

答案 1 :(得分:0)

所以按照Simmerman的建议,这里有一些PHP代码,如果我知道父文件夹duid(如根文件夹的'*'),我用它来获取子文件夹的duid。我已将以下函数添加到文件中提供的文件夹的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