百度網盤介面頻繁變更,此方案僅供學習參考,不保證長期有效。生產環境請使用官方 OpenAPI 或授權下載方式。
模擬瀏覽器訪問分享頁面,正則提取下載按鈕的真實鏈接,然後 302 重定向至該鏈接。
<?php
// downbd.php
$canshu = $_SERVER["QUERY_STRING"] ?? '';
if (empty($canshu)) {
die("參數缺失:需提供 shareid 和 uk");
}
$shareUrl = "https://pan.baidu.com/share/link?$canshu";
$html = @file_get_contents($shareUrl);
if ($html === false) {
die("無法獲取分享頁面,可能被風控或鏈接失效");
}
// 提取下載鏈接(頁面結構可能變動,需實時調整正則)
$pattern = '/class="dbtn cancel singledbtn" href=(.*?)id="downFileButtom"/is';
if (!preg_match($pattern, $html, $matches)) {
die("解析失敗:頁面結構已變更或文件不存在");
}
$rawUrl = $matches[1];
$rawUrl = str_replace(['"', '&'], ['', '&'], $rawUrl); // 清理轉義
// 安全檢查:確保是百度網盤域名
if (!preg_match('/^https?:\/\/[^/]*baidu\.com/', $rawUrl)) {
die("解析到非預期域名,疑似劫持");
}
header("Location: $rawUrl");
exit;
?>
https://your-domain.com/downbd.php?shareid=xxxxxx&uk=yyyyyy
file_get_contents 易被反爬蟲攔截(需偽造 User-Agent、Cookie、Referer)。/rest/2.0/xpan/file?method=download 獲取合法下載鏈接。<a download> 或 window.open。