百度网盘api curl截取返回302获取不到文件流,但是我在本地是可以的
// 获取文件下载地址
public function getFilepath(){
// $id = '212267407429337';
// $arr = $this->getFilemates($id);
$url = 'https://d.pcs.baidu.com/file/d56a8e7cf9436d61aebe866708e8bd7b?fid=1448984282-250528-802160331947795&rt=pr&sign=FDtAERV-DCb740ccc5511e5e8fedcff06b081203-GkL2EZeeTHiCs8hbDZZJoaNx%2Bf8%3D&expires=8h&chkbd=0&chkv=2&dp-logid=2501182078124797572&dp-callid=0&dstime=1603164499&r=954217416&access_token=121.0c096672f62e2826c66ab657e0105618.YCPkE3s6frC776rmE7hEpfrewn-LQrbqwYhC6oe.ziPCJg';
//$url = 'www.baidu.com';
$res = $this->http_get($url);
var_dump($res);
}
function http_get($url,$filename = '')
{
$headers[] = "User-Agent:pan.baidu.com";
$headers[] = " Accept-Encoding: gzip, deflate";
// $headers[] = "Zoomkey-Auth-Token: 9CD0F0F60AFDF00";
$curl = curl_init(); // 启动一个CURL会话
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 302 redirect
// curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
// curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
$tmpInfo = curl_exec($curl);
//关闭URL请求
curl_close($curl);
$res=mb_convert_encoding($tmpInfo, 'UTF-8', 'UTF-8,GBK,GB2312,BIG5');
// $downloaded_file = fopen('application/smapi/controller/data.docx', 'w'); // 此处文件扩展名 需要提前获取 动态拼接
// fwrite($downloaded_file, $tmpInfo);
// fclose($downloaded_file);
return $tmpInfo;
}