[PHP] 纯文本查看 复制代码
<?php
Class Alist
{
public $Api = 'https://alist.domain.com'; // 你的 alist 地址
private $UserName = 'test'; // 账号
private $PassWord = 'test123456'; // 密码
private $UserAgent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36';
// token获取
public function getToken(){
$cacheName = 'alist_'.$this->UserName.'_token';
if(is_file($cacheName)){
return file_get_contents($cacheName);
}
$Url = $this->Api.'/api/auth/login';
$Body = '{"username": "'.$this->UserName.'","password": "'.$this->PassWord.'"}';
$Data = json_decode($this->request($Url,'POST','',$Body),1);
if($Data['code'] == 200){
$Token = $Data['data']['token'];
file_put_contents($cacheName,$Token);
return $Token;
}else{
exit('ERROR:'.$Data['message']);
}
}
// 二选一
public function getToken2(){
$cacheName = 'alist_'.$this->UserName.'_token';
if(is_file($cacheName)){
return file_get_contents($cacheName);
}
$Url = $this->Api.'/api/auth/login/hash';
$Body = '{"username": "'.$this->UserName.'","password": "'.hash('sha256',$this->PassWord.'-https://github.com/alist-org/alist').'"}';
$Data = json_decode($this->request($Url,'POST','',$Body),1);
if($Data['code'] == 200){
$Token = $Data['data']['token'];
file_put_contents($cacheName,$Token);
return $Token;
}else{
exit('ERROR:'.$Data['message']);
}
}
// 获取当前用户信息
public function getUserInfo(){
$Url = $this->Api.'/api/me';
$Header = array(
'Authorization:'.$this->getToken(),
);
$Data = json_decode($this->request($Url,'GET',$Header),1);
return $Data;
}
// 新建文件夹
public function Mkdir($dir){
$Url = $this->Api.'/api/fs/mkdir';
$Header = array(
'Authorization:'.$this->getToken(),
'Content-Type:application/json',
);
$Body = '{"path": "'.$dir.'"}';
$Data = json_decode($this->request($Url,'POST',$Header,$Body),1);
return $Data;
}
// 重命名文件/目录
public function Rename($name, $path){
$Url = $this->Api.'/api/fs/rename';
$Header = array(
'Authorization:'.$this->getToken(),
'Content-Type:application/json',
);
$Body = '{"name": "'.$name.'","path": "'.$path.'"}';
$Data = json_decode($this->request($Url,'POST',$Header,$Body),1);
return $Data;
}
// 表单上传文件 需要目录已经存在(是否存在以alist缓存为准)
public function UpFile($file, $path){
$Url = $this->Api.'/api/fs/form';
$Header = array(
'Authorization:'.$this->getToken(),
//'Content-Length:'.$file['file']['size'],
'Content-Type: multipart/form-data',
'File-Path:'.urlencode($path.$file['name']),
);
$cfile = new CURLFile($file['tmp_name'],$file['type'],urlencode($file['name']));
$Ch = curl_init();
curl_setopt($Ch, CURLOPT_URL, $Url);
curl_setopt($Ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($Ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($Ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($Ch, CURLOPT_POSTFIELDS, array('file' => $cfile));
curl_setopt($Ch, CURLOPT_TIMEOUT, 60);
curl_setopt($Ch, CURLOPT_HTTPHEADER, $Header);
curl_setopt($Ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($Ch, CURLOPT_ENCODING, 'gzip');
$Output = curl_exec($Ch);
curl_close($Ch);
return json_decode($Output,1);
}
// POST 列出文件目录
public function list( $path, $password='', $page='', $per_page='', $refresh='false'){
$Url = $this->Api.'/api/fs/list';
$Header = array(
'Authorization:'.$this->getToken(),
);
$Body = '{"path": "'.$path.'","password": "'.$password.'","page": '.$page.',"per_page": '.$per_page.',"refresh": '.$refresh.'}';
$Data = json_decode($this->request($Url,'POST',$Header,$Body),1);
return $Data;
}
// POST 获取某个文件/目录信息
public function get( $path, $password='', $page='', $per_page='', $refresh='false'){
$Url = $this->Api.'/api/fs/get';
$Header = array(
'Authorization:'.$this->getToken(),
);
$Body = '{"path": "'.$path.'","password": "'.$password.'","page": '.$page.',"per_page": '.$per_page.',"refresh": '.$refresh.'}';
$Data = json_decode($this->request($Url,'POST',$Header,$Body),1);
return $Data;
}
private function request($Url, $Method="GET", $Header='', $Data='')
{
$Ch = curl_init();
$Header = $Header ? $Header : array("Content-Type:application/json;charset=utf-8;","Accept:application/json","charset=utf-8");
$Data = is_array($Data) ? json_encode($Data) : $Data;
curl_setopt($Ch, CURLOPT_URL, $Url);
curl_setopt($Ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($Ch, CURLOPT_SSL_VERIFYHOST, FALSE);
if($Method == "GET"){
curl_setopt($Ch, CURLOPT_CUSTOMREQUEST,"GET");
curl_setopt($Ch, CURLOPT_POST, false);
curl_setopt($Ch, CURLOPT_POSTFIELDS, $Data);
}
if($Method == "POST"){
curl_setopt($Ch, CURLOPT_CUSTOMREQUEST,"POST");
curl_setopt($Ch, CURLOPT_POST, true);
curl_setopt($Ch, CURLOPT_POSTFIELDS, $Data);
}
if($Method == "PUT"){
curl_setopt($Ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($Ch, CURLOPT_POSTFIELDS, $Data);
}
if($Method == "DELETE"){
curl_setopt($Ch, CURLOPT_CUSTOMREQUEST, "DELETE");
curl_setopt($Ch, CURLOPT_POSTFIELDS, $Data);
}
if($Method == "PATCH"){
curl_setopt($Ch, CURLOPT_CUSTOMREQUEST, "PATCH");
curl_setopt($Ch, CURLOPT_POSTFIELDS, $Data);
}
curl_setopt($Ch, CURLOPT_TIMEOUT, 10);
curl_setopt($Ch, CURLOPT_HTTPHEADER, $Header);
curl_setopt($Ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($Ch, CURLOPT_ENCODING, 'gzip');
$Output = curl_exec($Ch);
curl_close($Ch);
return $Output;
}
}
?>