2014-02-07 34 views
0

我想實現一個簡單的foldergrid php客戶端。我正在使用foldergrid api文檔中的簡單php客戶端。我從API文檔得到的客戶端代碼:foldergrid php客戶端獲取文件夾信息

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是我所創建的文件夾的名稱使用foldergrid javascript參考客戶端在https://mydomain.foldergrid.com/show/ *的.html

根文件夾名是相同的我的域名,這是我認爲是foldergrid的標準。

但是,當我運行該代碼時,我總是得到一個文件夾未找到響應。我嘗試過將文件夾路徑的各種不同排列作爲fetchFolder函數的輸入,但沒有成功。所以有兩個問題: 1.我應該如何使用php簡單客戶端中的fetchFolder函數來獲取文件夾信息 - 特別是duid。 2.有沒有人有一個額外的PHP簡單的客戶端來創建一個子文件夾,一旦我發現目標文件夾的duid?

THANKS

回答

0

當你create a new folder programmatically using the FolderGrid API分配不可改變的DUID唯一標識該文件夾。一個簡單的方法來確定DUID任何現有的文件夾是簡單地打開FolderGrid Web應用程序,然後右鍵單擊,選擇「顯示信息」的文件夾。

要使用API​​調用以編程方式確定未知的DUID,只需使用調用Show Folder Details來顯示所有子文件夾的DUID,即可從具有已知DUID的較高文件夾遍歷文件夾路徑。例如,您可以執行你的根文件夾中的GET /文件夾/ DUID將返回(除其他事項外)的根文件夾的所有子文件夾的名稱和DUIDs的JSON陣列 - 包括TestNewFolder

一個有用的快捷鍵在API文檔中提到的Show Folder Details這是你可以在調用代替DUID星號「*」作爲一個速記參考根文件夾。

示例PHP客戶端代碼使用名稱「pathOrDuid」作爲fetchFolder的參數,但這是誤導性的,因爲調用來檢索folder's details by path is a distinct call as documented here

0

所以以下Simmerman的建議,這裏是一些PHP代碼,我曾經得到一個子文件夾的DUID,如果我知道的父文件夾DUID(如「*」根文件夾)。我已經添加了以下功能給PHP簡單的客戶端foldergrid他們的文檔中提供:

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