一个简单的坚果云网盘接口,以前工作的时候写的,现在转行了,分享给大家。
上代码,都有注释。
davUrl = $davUrl : null;
isset($userName) ? $this->userName = $userName : null;
isset($passWord) ? $this->passWord = $passWord : null;
}
/*配置*/
private $davUrl = "https://dav.jianguoyun.com"; //网盘地址
private $userName = "xxx"; //用户名
private $passWord = "xxx"; //密码
/*分页*/
private $pageOffset = null; //第几页
private $pageLength = null; //每页数据量
public function page($p, $length) {
$this->pageOffset = $p;
$this->pageLength = $length;
return $this;
}
/**
* 外部的方法,方法中用到的“$folder”为文件夹路径,如“/dav/myfile/"
*/
/**
* @param $folder
* @return array
* 取得某个文件夹内的文件列表
*/
public function getFilelistByFolder($folder) {
$data = $this->curlPropfind($this->userName, $this->passWord, $this->davUrl . $folder);
$data = str_replace(
array("d:", " "),
array("", "collection"),
$data
);
$data = json_decode(
json_encode(
simplexml_load_string($data)
),
true
);
$data = $data["response"];
$fileList = array(); //准备文件列表
foreach ($data as $key => &$value) {
if (!$value["propstat"]['prop']["resourcetype"] == "collection") {
$fileList[] = array(
"url" => $value["href"],
"time" => strtotime($value["propstat"]['prop']["getlastmodified"]),
"type" => $value["propstat"]['prop']["getcontenttype"],
"name" => $value["propstat"]['prop']["displayname"],
);
}
}
//分页的实现
if (is_numeric($this->pageOffset) && is_numeric($this->pageLength)) {
$offset = ($this->pageOffset - 1) * $this->pageLength; //起始位置
if ($offset >= count($fileList)) {
return array();
}
$length = $this->pageLength; //长度
if (count($fileList) - $offset < $length) {
$length = count($fileList) - $offset < $length;
}
return array_slice($fileList, $offset, $length);
} else {
return $fileList;
}
}
/**
* @param $folder
* @return bool
* 创建文件夹
*/
public function createFolderByFolderName($folder) {
$data = $this->curlMkcol($this->userName, $this->passWord, $this->davUrl . $folder);
return true;
}
/**
* @param $folder
* @return bool
* 向某个文件夹上传文件
* 需要一个文件上传的form表单,当IS_POST动作时调用这个方法即可
*/
public function uploadFileByFolderName($folder) {
$data = $this->curlPut($this->userName, $this->passWord, $this->davUrl . $folder . urlencode($_FILES["file"]['name']), file_get_contents($_FILES["file"]['tmp_name']));
return true;
}
/**
* @param $url 路径
* @param $fileName 文件名
* @return string
* 文件下载
*/
public function downloadFileByUrl($url, $fileName) {
header("Content-Type: application/octet-stream");
header("Accept-Ranges: bytes");
header("Content-Disposition: attachment; filename=" . $fileName);
$data = $this->curlGet($this->userName, $this->passWord, $this->davUrl . $url);
echo $data;
}
/**
* @param $url
* @return bool
* w文件删除
*/
public function deleteFileByUrl($url) {
$data = $this->curlDelete($this->userName, $this->passWord, $this->davUrl . $url);
return true;
}
/**
* @param $username
* @param $password
* @param $url
* @return bool|mixed
* PROPFIND请求
*/
private function curlPropfind($username, $password, $url) {
$ch = curl_init();
$header = array(
'Content-Type: text/xml',
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PROPFIND");
curl_setopt($ch, CURLOPT_TIMEOUT, 30); //30秒超时
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); //get status code
$result = curl_exec($ch);
if (curl_error($ch)) {
//curl_error($ch);//错误原因
curl_close($ch);
return false;
} else {
curl_close($ch);
return $result;
}
}
/**
* @param $username
* @param $password
* @param $url
* @return bool|mixed
* MKCOL请求
*/
private function curlMkcol($username, $password, $url) {
$ch = curl_init();
$header = array(
'Content-Type: text/xml',
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_HEADER, 1); //返回response头部信息
curl_setopt($ch, CURLINFO_HEADER_OUT, true); //TRUE 时追踪句柄的请求字符串,从 PHP 5.1.3 开始可用。这个很关键,就是允许你查看请求header
curl_setopt($ch, CURLOPT_URL, $url); //请求的url
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "MKCOL"); //请求模式
curl_setopt($ch, CURLOPT_TIMEOUT, 30); //30秒超时
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //获取的信息以字符串返回,而不是直接输出。
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password"); //用户名和密码
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); //get status code
$status_head = curl_getinfo($ch, CURLINFO_HEADER_OUT); //头信息
$result = curl_exec($ch);
if (curl_error($ch)) {
//curl_error($ch);//错误原因
curl_close($ch);
return false;
} else {
curl_close($ch);
return $result;
}
}
/**
* @param $username
* @param $password
* @param $url
* @param $data
* @return bool|mixed
* Put请求
*/
private function curlPut($username, $password, $url, $data) {
$ch = curl_init();
$header = array(
'Content-Type: text/xml',
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_HEADER, 1); //返回response头部信息
curl_setopt($ch, CURLINFO_HEADER_OUT, true); //TRUE 时追踪句柄的请求字符串,从 PHP 5.1.3 开始可用。这个很关键,就是允许你查看请求header
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_TIMEOUT, 30); //30秒超时
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); //get status code
$result = curl_exec($ch);
if (curl_error($ch)) {
//curl_error($ch);//错误原因
curl_close($ch);
return false;
} else {
curl_close($ch);
return $result;
}
}
/**
* @param $username
* @param $password
* @param $url
* @return bool|mixed
* Get请求
*/
private function curlGet($username, $password, $url) {
$ch = curl_init();
$header = array(
'Content-Type: text/xml',
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_TIMEOUT, 30); //30秒超时
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); //get status code
$result = curl_exec($ch);
if (curl_error($ch)) {
//curl_error($ch);//错误原因
curl_close($ch);
return false;
} else {
curl_close($ch);
return $result;
}
}
/**
* @param $username
* @param $password
* @param $url
* @return bool|mixed
* DELETE请求
*/
private function curlDelete($username, $password, $url) {
$ch = curl_init();
$header = array(
'Content-Type: text/xml',
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_HEADER, 1); //返回response头部信息
curl_setopt($ch, CURLINFO_HEADER_OUT, true); //TRUE 时追踪句柄的请求字符串,从 PHP 5.1.3 开始可用。这个很关键,就是允许你查看请求header
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
curl_setopt($ch, CURLOPT_TIMEOUT, 30); //30秒超时
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); //get status code
$result = curl_exec($ch);
if (curl_error($ch)) {
//curl_error($ch);//错误原因
curl_close($ch);
return false;
} else {
curl_close($ch);
return $result;
}
}
}