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);
        $html = curl_exec($ch);
        if (preg_match('/bdstoken="(.*?)".*?fsId="(.*?)".*?share_uk="(.*?)".*?share_id="(.*?)"/i', $html, $matches)) {
                $proxyUrl  = sprintf('http://pan.baidu.com/share/download?bdstoken=%s&uk=%s&shareid=%s&fid_list=%%5B%s%%5D', $matches[1], $matches[3], $matches[4], $matches[2]);
                curl_setopt($ch, CURLOPT_URL, $proxyUrl);
                $proxyHtml = curl_exec($ch);
                $jsonObj   = json_decode($proxyHtml, true);
                $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/share/link?shareid=3142223074&uk=1864945200';
$downurl = baiduPan($testurl);
//这个就是文件的真实下载地址,可以用浏览器或者下载工具试试
echo $downurl; 


规则是活的,代码是死的,所以无法保证可以永久使用,如发现无法正常使用,那就是网盘规则变了,需要自行修改代码咯,就个人经验来看,百度网盘一般两到三个月会更新一次,不过变化不会太大。

Related post

微信公众号:程序员到架构师

最新文章

Return Top