forked from dylanbai8/bp3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdn.php
60 lines (52 loc) · 1.83 KB
/
dn.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php
session_start();
/**
* 文件下载模块,访客权限可用
*/
// 1.获取fsid
$fsid = $_GET['fsid'];
if(empty($fsid)){
echo '无效fsid';
die;
}
require_once('./config.php');
// 2.查询下载链接
$access_token = $config['identify']['access_token'];
$url = "http://pan.baidu.com/rest/2.0/xpan/multimedia?access_token=$access_token&method=filemetas&fsids=[$fsid]&dlink=1&thumb=1&dlink=1&extra=1";
$opts = array(
'http' => array(
'method' => 'GET',
'header' => 'User-Agent: pan.baidu.com'
));
$context = stream_context_create($opts);
$result = @file_get_contents($url, false, $context);
$json = json_decode($result);
$dlink = $json->list[0]->dlink;
$file_size = $json->list[0]->size;
$file_name = $json->list[0]->filename;
// 3.下载文件
//get请求参数
$request_data = array('access_token' => $access_token);
//初始化curl对象
$ch = curl_init();
//设置URL
curl_setopt($ch, CURLOPT_URL, $dlink);
// 直接返回给浏览器
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
// 设置access_token
curl_setopt($ch, CURLOPT_POSTFIELDS, $request_data);
// 设置user-agent
curl_setopt($ch, CURLOPT_USERAGENT,"pan.baidu.com");
// 允许301,302
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
// 取消https验证
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
// 返回header给浏览器,并执行curl
header("Content-type: application/octet-stream");
header("Accept-Ranges: bytes");
header("Content-Disposition: attachment; filename=$file_name");
header("Content-Length: $file_size");
header('Content-Transfer-Encoding: binary');
curl_exec($ch);
?>