curl下载文件

* curl下载文件
* 根据业务需求
* 通过不同站点去访问路径
* 下载文件
* 但是不同站点需要设置header头
* 这里使用curl方式下载
* 具体看代码:
 1 //下载地址
 2 $url = 'https://xxxxx.com/download/xxxxx';
 3 //header请求头  [主要用于将请求模拟为浏览器发送请求]
 4 $header = array(
 5     'xxxx:xxxxxx',//自定义header头
 6     'xxxx: xxxxx',
 7     //模拟浏览器访问   可以将浏览器中的请求复制出来进行处理
 8     'accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
 9     'accept-encoding: gzip, deflate, br',
10     'accept-language: zh-CN,zh;q=0.9',
11     'user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36',
12 );
13 //文件存储路径
14 $filename = '/Users/kinsfeng/Desktop/xxx.zip';
15 $ch = curl_init();
16 $timeout = 10;
17 curl_setopt($ch, CURLOPT_URL, $url);
18 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
19 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
20 curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
21 $file = curl_exec($ch);
22 curl_close($ch);
23 
24 // 保存文件到制定路径
25 file_put_contents($filename, $file);
26 
27 unset($file, $url);
28 return true;

 

转载于:https://www.cnblogs.com/kinsFeng/p/10021317.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值