baidu 云盘 php下载代码,还需要测试

这是一个PHP函数,用于从百度网盘下载文件。它首先获取必要的参数,然后通过构造下载链接并进行HTTP请求来下载文件。需要注意的是,代码中包含了一些可能需要测试和调整的部分,例如设置User-Agent和处理重定向。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<?php
function baiduPan($url) {
	if (strpos($url, 'pan.baidu.com') === false) return false;
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_HEADER, false);
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_VERBOSE, true);
	/*
	   $cookiefile = "cookie.txt";
	   curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiefile);
	   curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiefile);

	   curl_close($ch);
	   $ch = curl_init();
	   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	   curl_setopt($ch, CURLOPT_HEADER, false);
	   curl_setopt($ch, CURLOPT_VERBOSE, true);
	   curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiefile);
	   curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiefile);

	 */
	$html = curl_exec($ch);
	file_put_contents("out3.txt", $html);

	if (preg_match('/fs_id\\\\":\\\\"(.*?)\\\\"/i', $html, $m)) {
		$fsid = $m[1];
	preg_match('/shareid="(.*?)"/i', $html, $m);
	$shareid = $m[1];
	preg_match('/.uk="(.*?)"/i', $html, $m);
	$uk = $m[1];
	preg_match('/timestamp="(.*?)"/i', $html, $m);
	$timestamp = $m[1];

	curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:19.0) Gecko/20100101 Firefox/19.0");
	curl_setopt($ch, CURLOPT_HTTPHEADER, array("X-Requested-With: XMLHttpRequest", "Accept: application/json"));
	$referer = $url;
	curl_setopt($ch, CURLOPT_REFERER, $referer);

	preg_match('/sign="(.*?)"/i', $html, $m);
	$share_sign = $m[1];
	$proxyUrl  = sprintf('http://pan.baidu.com/share/download?uk=%s&shareid=%s&fid_list=%s&sign=%s', 
			$uk, $shareid, urlencode("[") . $fsid . urlencode("]"), $share_sign);
	$proxyUrl .= sprintf("×tamp=%s", $timestamp);
	var_dump($fsid);
	$proxyUrl .= sprintf("&r=%s", rand(0, 1000000000) / 1000000000);
	curl_setopt($ch, CURLOPT_URL, $proxyUrl);
	$proxyHtml = curl_exec($ch);
	$jsonObj   = json_decode($proxyHtml, true);
	var_dump($proxyHtml);exit();
	$downloadButtonUrl = $jsonObj['dlink'];
	$downloadButtonUrl = str_replace("\\\\/", "/", $downloadButtonUrl);
	curl_setopt($ch, CURLOPT_URL, $downloadButtonUrl);
	curl_setopt($ch, CURLOPT_VERBOSE, 1);
	curl_setopt($ch, CURLOPT_HEADER, 1);
	$result = curl_exec($ch);
	list($header, $body) = explode("\r\n\r\n", $result, 2);
	if (preg_match('/Location: (.*?)(\r?\n)/is', $header, $matches)) {
		return $matches[1];
	}
}
return false;
}
//$testurl = 'http://pan.baidu.com/wap/link?shareid=3142223074&uk=1864945200';
$testurl = 'http://pan.baidu.com/wap/link?shareid=2532037509&uk=1177880683';
$downurl = baiduPan($testurl);
echo $downurl; 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值