andrea11/api-filerun

此包的最新版本(dev-master)没有可用的许可证信息。

FileRun API 客户端库

dev-master / 1.0.x-dev 2016-07-07 13:20 UTC

This package is not auto-updated.

Last update: 2024-09-20 19:31:35 UTC


README

此库展示了如何在自己的 PHP 应用程序中使用 FileRun API。它使用 OAuth2 "资源所有者凭证流",用户的凭证存储在您的应用程序中。

有关更多信息,请阅读 FileRun API 文档

安装

composer require filerun/api-client:dev-master

使用

$FileRun = new FileRun\API\Client(array(
	'url' => 'http://WWW.YOUR-SITE.COM/FILERUN',
	'client_id' => 'GET-THIS-FROM-THE-FILERUN-CONTROL-PANEL',
	'client_secret' => 'GET-THIS-FROM-THE-FILERUN-CONTROL-PANEL',
	'username' => 'admin', //the FileRun username
	'password' => 'admin', //the FileRun password
	'scope' => ['profile', 'list', 'upload', 'download', 'weblink', 'delete', 'share'] //list of "permissions" needed to complete the operations
));
$rs = $FileRun->connect();
if (!$rs) {exit('Failed to connect');}
$FileRun->debug = true;//set this to troubleshoot possible errors

这将验证您的程序并允许您调用 API

获取用户信息

$userInfo = $FileRun->getUserInfo();
if (!$userInfo) {
	exit('Failed to get user info: '.$FileRun->getError());
}
echo 'Hello '.$userInfo['name'].'!<br>';

上传文件

$data = 'This is the file contents. This is some unique data: '.time().'-'.rand();
$rs = $FileRun->uploadFile(['path' => '/ROOT/HOME/MyUploadedFile.txt'], $data);
if ($rs && $rs['success']) {
	echo 'File successfully uploaded with the following contents:<br>';
	echo '<div style="border:1px solid silver;background-color:whitesmoke;padding:5px;">'.$data.'</div>';
} else {
	exit('Failed to upload file: '.$FileRun->getError());
}

列出文件夹内容

$rs = $FileRun->getFileList(['path' => '/ROOT/HOME']);
if ($rs && $rs['success']) {
	echo 'Here is the list of files and folders inside your home folder:<br>';
	echo '<div style="border:1px solid silver;background-color:whitesmoke;padding:5px;max-height:200px;overflow:auto"><pre>';
	print_r($rs);
	echo '</pre></div>';
} else {
	exit('Failed to retrieve list of files: '.$FileRun->getError());
}

搜索文件

$rs = $FileRun->searchFiles(['path' => '/ROOT/HOME', 'keyword' => 'MyUploaded']);
if ($rs && $rs['success']) {
	echo 'Search results for keyword "MyUploaded":<br>';
	echo '<div style="border:1px solid silver;background-color:whitesmoke;padding:5px;max-height:200px;overflow:auto"><pre>';
	print_r($rs);
	echo '</pre></div>';
} else {
	exit('Failed to retrieve search result: '.$FileRun->getError());
}

下载文件

$rs = $FileRun->downloadFile(['path' => '/ROOT/HOME/MyUploadedFile.txt']);
if ($rs) {
	echo 'File successfully downloaded with the following contents:<br>';
	echo '<div style="border:1px solid silver;background-color:whitesmoke;padding:5px;">'.$rs.'</div>';
} else {
	exit('Failed to download file: '.$FileRun->getError());
}

创建网页链接

$rs = $FileRun->getWebLink(['path' => '/ROOT/HOME/MyUploadedFile.txt']);
if ($rs && $rs['success']) {
	echo 'WebLink generated: <a href="'.$rs['data']['linkInfo']['url'].'" target="_blank">'.$rs['data']['linkInfo']['url'].'</a>';
	echo '<br>';
} else {
	exit('Failed to get weblink: '.$FileRun->getError());
}

共享文件夹

$rs = $FileRun->shareFolder([
	'path' => '/ROOT/HOME/MyFolder',
	'uid' => 123, //share with user ID 123
	//'gid' => 456, //share with group ID 456
	'anonymous' => 0,   //set to 1 for anonymous share
	'upload' => 1,      //set to 0 to disable uploads
	'download' => 1,    //set to 0 to disable downloads
	'comment' => 1,     //set to 0 to prevent users from posting comments
	'read_comments' => 1, //set to 0 to prevent users from reading comments
	'alter' => 0,       //set to 1 to allow users to rename/delete/move/etc files
	'share' => 0,       //set to 1 to allow users to create web links inside the share
	'alias' => 'My share' //If you want the folder to be shared with a different name
]);
if ($rs && $rs['success']) {
	echo 'Folder successfully shared.';
} else {
	exit('Failed to share folder: '.$FileRun->getError());
}

取消共享文件夹

$rs = $FileRun->unShareFolder([
	'path' => '/ROOT/HOME/MyFolder',
	'uid' => 123, //unshare with user ID 123
	//'gid' => 456, //unshare with group ID 456
]);
if ($rs && $rs['success']) {
	echo 'Folder successfully unshared.';
} else {
	exit('Failed to unshare folder: '.$FileRun->getError());
}

删除文件

$rs = $FileRun->deleteFile(['path' => '/ROOT/HOME/MyUploadedFile.txt']);
if ($rs && $rs['success']) {
	echo 'File successfully moved to trash.';
} else {
	exit('Failed to move file to trash: '.$FileRun->getError());
}