foldergrid php客户端获取文件夹信息
我正在尝试实现一个简单的foldergrid php客户端。我正在使用foldergrid api文档中的简单php客户端。我从api文档中获得的客户端代码是:foldergrid php客户端获取文件夹信息,php,grid,client,directory,Php,Grid,Client,Directory,我正在尝试实现一个简单的foldergrid php客户端。我正在使用foldergrid api文档中的简单php客户端。我从api文档中获得的客户端代码是: class FolderGridClient { protected $_cookieFileLocation = './cookie.txt'; public $_webpage; public $_status; public $_location; const SERVER_HOS
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>";
}
$uname='myuname';
$pwd='mypwd';
$domain='mydomain';
$rootDUID='duidOFRootFolder';
$client=新FolderGridClient;
$client->authenticate($uname、$pwd、$domain);
如果($client->_status<400){
回显“fetchFolder:”.$client->fetchFolder($domain.'/TestNewFolder')。“
”;
}
TestNewFolder是我在*.html使用foldergrid javascript参考客户端创建的文件夹的名称
根foldername与我的域名相同,我认为这是foldergrid的标准
然而,当我运行该代码时,我总是得到一个文件夹未找到的响应。我尝试了各种不同的文件夹路径排列作为fetchFolder函数的输入,但没有成功。所以有两个问题:
1.我应该如何在php简单客户端中使用fetchFolder函数来获取文件夹信息——特别是duid。
2.在我发现目标文件夹的duid后,是否有人在php简单客户机中添加了一个子文件夹来创建子文件夹
感谢您分配唯一标识该文件夹的不可变DUID。确定任何现有文件夹的DUID的一种简单方法是,只需打开FolderGrid Web应用程序,右键单击该文件夹以选择“显示信息”
要使用API调用以编程方式确定未知DUID,只需使用显示所有子文件夹DUID的调用,从具有已知DUID的较高文件夹遍历文件夹路径。例如,在根文件夹上执行GET/folder/DUID,它将返回(除其他外)根文件夹所有子文件夹(包括TestNewFolder)的名称和DUID的json数组
API文档中提到的一个有用的快捷方式是,您可以在调用中用星号“*”替换DUID,作为对根文件夹的速记引用
示例PHP客户机代码使用名称“pathrdud”作为fetchFolder的参数,但这是误导性的,因为调用来检索一个文件。所以根据Simmerman的建议,下面是一些PHP代码,如果我知道父文件夹的duid(如根文件夹的“*”),我就用它来获取子文件夹的duid。我向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>";
}
if($client->_status<400){
$parentDuid='*';
$folderName='TestNewFolder';
$folderDuid=$client->getSubFolderDuid($parentDuid,$folderName);
echo“$folderName:$folderDuid
”;
}
有了这些构建块,我现在可以构建代码来遍历目录树,或者在树的任意深度找到特定的文件夹duid
谢谢你给我指明了正确的方向Simmerman