百度网盘开放平台接口,上传、下载等功能PHP详细操作

目录

1、获取code

2、获取access_token

3、获取网盘容量

4、获取文件列表

5、预上传

6、分片上传

7、创建文件


1、获取code

    public function getCode(Request $request)
    {
        $config = [
            'client_id' => 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
            'redirect_uri' => '你的回调地址',
            'state' => md5(uniqid())
        ];
        $url = 'https://openapi.baidu.com/oauth/2.0/authorize?response_type=code&client_id='. $config['client_id'].'&redirect_uri='. $config['redirect_uri'].'&scope=netdisk&display=mobile&qrcode=1&force_login=0&device_id=&state='.$config['state'];
        //生成二维码图片
        $qrcode = QrCode::encoding('UTF-8')->size(300)->generate($url);
        $this->success('OK', [
            'qrcode' => base64_encode($qrcode),
            'url' => $url
        ]);
    }

2、获取access_token

 public function getAuth(Request $request)
    {
        $code = $request->input('code', '');
        if(empty($code)){
            exit();
        }
        $config = [
            'client_id' => 'XXXXXXXXXXXXXXXXXXXXXXXX',
            'SecretKey' => 'XXXXXXXXXXXXXXXXXXXXXXXX',
            'redirect_uri' => '回调地址',
        ];
        $url = 'https://openapi.baidu.com/oauth/2.0/token?grant_type=authorization_code&code='.$code.'&client_id='.$config['client_id'].'&client_secret='.$config['SecretKey'].'&redirect_uri=' . $config['redirect_uri'].'&state=get_list';
        $result = $this->curl_get($url);
        $access_token = json_decode($result,true);

    }

3、获取网盘容量

    public function getQuota()
    {
        $url = 'https://pan.baidu.com/api/quota?access_token='.$this->config['access_token'].'&checkfree=1&checkexpire=1';
        $result = Http::withOptions(['verify' => false])->get($url);
        $res = $result->body();
        $res = \GuzzleHttp\json_decode($res);
        if($res->errno === 0){
            $this->success('OK',[
                'total' => $res->total / 1024 / 1024 / 1024 . 'GB',
                'free' => $res->free / 1024 / 1024 / 1024 . 'GB',
                'used' => $res->used / 1024 / 1024 / 1024 . 'GB',
                'remain' => ($res->total - $res->used) / 1024 / 1024 / 1024 . 'GB',
                'expire' => $res->expire,
            ]);
        }
        $this->error($this->msg[$res->errno]);
    }

4、获取文件列表

public function getList($page)
    {
        $start = ($page - 1) * 10;
        $data = [
            'method' => 'list',
            'dir' => '/',
            'access_token' => $this->config['access_token'],
            'order' => 'time',
            'web' => 1,
        ];
        $total = \GuzzleHttp\json_decode(Http::withOptions(['verify' => false])->get($this->api.http_build_query($data))->body());
        $data['start'] = $start;
        $data['limit'] = 10;
        $result = Http::withOptions(['verify' => false])->get($this->api.http_build_query($data));
        $res = $result->body();
        $res = \GuzzleHttp\json_decode($res);
        if($res->errno === 0){
            $list = [];
            if(count($res->list) > 0){
                foreach($res->list as $key => $item){
                    $list[] = [
                        'id' => $key + 1,
                        'name' => $item->server_filename,
                        'create_time' => date('Y-m-d H:i:s', $item->server_ctime),
                        'size' => $item->isdir === 1 ? '文件夹' : $item->size / 1024 / 1024 / 1024
                    ];
                }
            }
            $this->success('OK',[
                'list' => $list,
                'total' => count($total->list) ?? 0
            ]);
        }
        $this->error($this->msg[$res->errno]);
    }

5、预上传

public function precreate()
    {
        $url = $this->api . 'method=precreate&access_token=' . $this->config['access_token'];
        $data = [
            'path' => urlencode('/apps/XXXXXX/index.txt'),
            'size' => filesize(base_path().'/public/index.txt'),
            'isdir' => 0,
            'autoinit' => 1,
            'block_list' => \GuzzleHttp\json_encode([md5('index.php')])
        ];
        $result = Http::asForm()->withOptions(['verify' => false])->post($url, $data);
        $res = $result->body();
        $res = \GuzzleHttp\json_decode($res);
        print_r($res);
        if($res->errno === 0){
            $res = $this->upload($res->uploadid, $res->path, $res->block_list);
        }
        $this->error($this->msg[$res->errno]);
    }

6、分片上传

private function upload($uploadid, $path, $block_list)
    {
        $url = 'https://d.pcs.baidu.com/rest/2.0/pcs/superfile2?';
        $data = [
            'method' => 'upload',
            'access_token' => $this->config['access_token'],
            'type' => 'tmpfile',
            'path' => $path,
            'uploadid' => $uploadid,
            'partseq' => 0
        ];
        $url .= http_build_query($data);
        $result = Http::attach('robots', base_path().'/public/index.txt')->withOptions(['verify' => false])->post($url, [
            'file' => base_path().'\public\index.php'
        ]);
        $res = $result->body();
        $res = \GuzzleHttp\json_decode($res);
        if($res->md5){
            $this->create($path, $uploadid, $res->md5);
        }
    }

7、创建文件

private function create($path, $uploadid, $md5)
    {
        $url = $this->api. 'method=create&access_token=' . $this->config['access_token'];
        $data = [
            'path' => '/apps/xxxxxx/index.txt',
            'size' => filesize(base_path().'/public/index.txt'),
            'isdir' => '0',
            'rtype' => 1,
            'block_list' => \GuzzleHttp\json_encode([$md5]),
            'uploadid' => $uploadid,
            'mode' => 3
        ];
 
        $result = Http::withOptions(['verify' => false])->post($url, $data);
        $res = $result->body();
        $res = \GuzzleHttp\json_decode($res);
        if($res->errno === 0){

        }
    }

我目前卡在了这里,创建文件,返回

stdClass Object
(
    [errno] => 2
    [path] => 
    [request_id] => 254240173748038691
)

查阅了很多资料和大佬的文章,都没对这个有个明确的说法!

PS:这个问题,在今天突然解决了,这个block_list参数,在文档中虽然写这不是必须,但是实际上却是必须传的,其实和第一个预创建的格式和数据是一样的。

private function create($path, $uploadid, $md5)
    {
        $url = $this->api. 'method=create&access_token=' . $this->config['access_token'];
        $data = [
            'path' => $path,
            'size' => filesize(base_path().'/public/index.zip'),
            'isdir' => 0,
            'rtype' => 1,
            'block_list' => \GuzzleHttp\json_encode([$md5]),
            'uploadid' => $uploadid,
            'mode' => 3
        ];
        $result = Http::asForm()->withOptions(['verify' => false])->post($url, $data);
        $res = $result->body();
        $res = \GuzzleHttp\json_decode($res);
        if($res->errno === 0){

        }
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值