坚果云网盘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 = "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:", "<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) {
		$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;
		}
	}
}

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值