php_curl Dropbox api

function create_dropbox_folder($foldername)
{
    if(isset($foldername)){
    //查询文件
    $data=[
        'query'=>$foldername,//文件名称
        'options'=>['path'=>""]//多选字段 path 文件路径
    ];
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_URL, 'https://api.dropboxapi.com/2/files/search_v2');
    curl_setopt($ch, CURLOPT_HTTPHEADER, [
       "Content-Type: application/json",
       'Authorization: Bearer access_token'
   ]);
    if($data) {
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
    }
    curl_setopt ($ch, CURLOPT_CUSTOMREQUEST, "POST");
    $result = curl_exec($ch);
    curl_close($ch);
   if(empty(json_decode($result)->matches))
   {
        $path=[
            'paths'=>
            [文件路径:路径后面得有文件名称]
        ];
         // 创建多个文件
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_URL, 'https://api.dropboxapi.com/2/files/create_folder_batch');
        curl_setopt($ch, CURLOPT_HTTPHEADER, [
           "Content-Type: application/json",
           'Authorization: Bearer access_token'
       ]);
        if($path) {
            curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($path));
        }
        curl_setopt ($ch, CURLOPT_CUSTOMREQUEST, "POST");
        $result = curl_exec($ch);
        curl_close($ch);
           $paths=[
            'entries'=>[
                [
               'from_path'=>"复制的dropbox路径",
               'to_path'=>"粘贴到的文件夹路径"
               ],
            ],
                "autorename"=>true //自动命名true 是 false 否
            ];
        // 复制文件多个文件 获取异步操作id
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_URL, 'https://api.dropboxapi.com/2/files/copy_batch_v2');
        curl_setopt($ch, CURLOPT_HTTPHEADER, [
           "Content-Type: application/json",
           'Authorization: Bearer access_token'
       ]);
        if($paths) {
            curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($paths));
        }
        curl_setopt ($ch, CURLOPT_CUSTOMREQUEST, "POST");
        $results = curl_exec($ch);
        curl_close($ch);
		//执行异步方法 async_job_id异步方法id
        if(isset(json_decode($results,true)['async_job_id'])){
            $data=['async_job_id'=>json_decode($results,true)['async_job_id']];
              $ch = curl_init();
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_URL, 'https://api.dropboxapi.com/2/files/copy_batch/check_v2');
            curl_setopt($ch, CURLOPT_HTTPHEADER, [
               "Content-Type: application/json",
               'Authorization: Bearer access_token'
           ]);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
            if($data) {
                curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
            }
            curl_setopt ($ch, CURLOPT_CUSTOMREQUEST, "POST");
            $ss = curl_exec($ch);
            curl_close($ch);
            }
        }
    }
}
//列出文件列表
   //查询文件
    $data=[
        'path'=>""//文件路径
    ];
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_URL, 'https://api.dropboxapi.com/2/files/list_folder');
    curl_setopt($ch, CURLOPT_HTTPHEADER, [
       "Content-Type: application/json",
       'Authorization: Bearer access_token'
   ]);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    if($data) {
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
    }
    curl_setopt ($ch, CURLOPT_CUSTOMREQUEST, "POST");
    $result = curl_exec($ch);
    curl_close($ch);
		//上传文件
		$www=$_FILES['file'];
	     $scr = $www['tmp_name'];
        $ext = substr($www['name'],strrpos($www['name'],'.')+1); // 上传文件后缀
        $path = $www; 
		$fp = fopen($scr, 'rb');
		$size = filesize($scr);
		$path1=['path'=>"/".$www['name'].'.'.$ext];//文件路径
		$cheaders = array('Authorization: Bearer access_token',
		                  'Content-Type: application/octet-stream',
		                  'Dropbox-API-Arg: '.json_encode($path1));
		$ch = curl_init('https://content.dropboxapi.com/2/files/upload');
		curl_setopt($ch, CURLOPT_HTTPHEADER, $cheaders);
		curl_setopt($ch, CURLOPT_PUT, true);
		curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
		curl_setopt($ch, CURLOPT_INFILE, $fp);
		curl_setopt($ch, CURLOPT_INFILESIZE, $size);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		$response = curl_exec($ch);
		curl_close($ch);
		fclose($fp);
	//下载文件到浏览器
    header('Content-Description: File Transfer');
    header('Content-Type: application/vnd.android.package-archive');
    header('Content-Disposition: attachment; filename=下载到本地的文件夹路径');
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Pragma: public');

$data=[
        'path'=>"下载文件路径"
    ];
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_URL, 'https://content.dropboxapi.com/2/files/download');
    curl_setopt($ch, CURLOPT_HTTPHEADER, [
       'Authorization: Bearer access_token',
       'Dropbox-API-Arg:'.json_encode($data)
   ]);
    curl_setopt($ch, CURLOPT_WRITEFUNCTION, function ($ch, $buffer) {
        echo $buffer;
        return strlen($buffer);
    });
    curl_setopt ($ch, CURLOPT_CUSTOMREQUEST, "POST");
	$result = curl_exec($ch);
    curl_close($ch);





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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值