curl下载pdf
# 远程调用携带 Token 下载 PDF
if (!function_exists('downFile')) {
/**
* 下载顺丰云打印面单远程PDF
* @param $url string 远端文件路径
* @param $savePath string 保存本地文件路径
* @param $token string token
* @return string
*/
function downFile($url, $savePath, $token)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//关闭对等证书
curl_setopt($ch, CURLOPT_URL, $url);//请求地址
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'X-Auth-token: '.$token
));
curl_setopt($ch, CURLOPT_HEADER, true);//返回头部信息
//需要response header
curl_setopt($ch, CURLOPT_NOBODY, false);//需要response body
$response = curl_exec($ch);
//分离header与body
$header = '';
$body = '';
if (curl_getinfo($ch, CURLINFO_HTTP_CODE) == '200') {
$headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE); //头信息size
$header = substr($response, 0, $headerSize);
$body = substr($response, $headerSize);
}
curl_close($ch);
//文件名
if ('' != $body) {
$file = date('YmdHis').'.pdf';
$fullName = rtrim($savePath, '/').'/'.$file;
//创建目录并设置权限
$basePath = dirname($fullName);
if (!file_exists($basePath)) {
@mkdir($basePath, 0777, true);
@chmod($basePath, 0777);
}
if (file_put_contents($fullName, $body)) {
return $file;
}
}
return false;
}
}
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
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
注意
这里的$file
生成的文件名如果在多个 PDF 下可能会重复,需要进行生成不重复的名称
编辑 (opens new window)
上次更新: 2022/07/21, 00:05:27