8wDlpd.png
8wDFp9.png
8wDEOx.png
8wDMfH.png
8wDKte.png
PHPwebdav操作示例 PHP
admin 2022-9-3

PHP下载,上传坚果云的文件。

<?php
namespace Cn\Lib;
 
/**
 * Class JianguoyunLib
 * @package Cn\Lib
 * 坚果云网盘接口
 * by sdxjwkq
 */
class JianguoyunLib {
	/**
	 * JianguoyunLib constructor.
	 * @param $davUrl
	 * @param $userName
	 * @param $passWord
	 * 构造
	 */
	public function __construct($davUrl, $userName, $passWord) {
		isset($davUrl) ? $this->davUrl = $davUrl : null;
		isset($userName) ? $this->userName = $userName : null;
		isset($passWord) ? $this->passWord = $passWord : null;
	}
	/*配置*/
	private $davUrl = "https://dav.jianguoyun.com"; //网盘地址
	private $userName = "xxxx"; //用户名
	private $passWord = "xxxx"; //密码
	/*分页*/
	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:", "<collection/>"),
			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,$fileName,$localfile) {
		$data = $this->curlPut($this->userName, $this->passWord, $this->davUrl . $folder . urlencode($fileName), file_get_contents($localfile));
		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 路径
	 * @param $fileName 文件名
	 * @return string
	 * 文件保存到本地
	 */
	public function saveFileByUrl($url, $fileName, $path='./files/') {
		$data = $this->curlGet($this->userName, $this->passWord, $this->davUrl . $url);
		
        // 判断文件是否存在
        if(!file_exists($path)){
        	// 创建文件夹
        	//创建一个有读写权限的目录,最好使用以下代码,不要直接使用mkdir函数指定权限,以避免系统umask的影响,要注意一点,权限值最好使用八进制表示,即“0”开头,而且一定不要加引号。
        	$result = mkdir($path, 0754);
        	$result = chmod($path, 0754);
        }
        
        $file_name = $path.$fileName;
        // 写入方式打开文件,fopen有多种打开方式
        $text_file = fopen($file_name, "w");
        // //这里一定要用"/r/n"才能正确换行
        fwrite($text_file, $data);
        // 关闭文件
        fclose($text_file);
	} 
 
	/**
	 * @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) {
	    // echo $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;
		}
	}
}
$davUrl = "https://dav.jianguoyun.com"; //网盘地址
$userName = "xxxx"; //用户名
$passWord = "xxxx"; //密码

$a = new JianguoyunLib($davUrl,$userName ,$passWord);
$b = $a -> getFilelistByFolder('/dav/SparrowNote/');

echo '<pre>';
print_r($b[count($b)-1]);
echo '</pre>';

$url = $b[count($b)-1]['url'];
$filename = $b[count($b)-1]['name'];
$a -> saveFileByUrl($url,$filename);
最后于 2022-9-3 被admin编辑 ,原因:
最新回复 (1)
    • 朕弟分享 | 专注小众,乐于分享!
      3
          
返回
发新帖 搜索 反馈 回顶部