Bootstrap

获取蓝奏云直链工具解析详解(无需密码)

蓝奏云是国内一个非常受欢迎的文件分享平台,但其链接并不是直接的下载链接,而是需要通过一定的解析步骤获取真实下载地址。本文将详细讲解一个PHP实现的蓝奏云直链获取工具的工作原理及其代码实现。

功能介绍

这个工具的主要功能是解析蓝奏云分享链接,获取文件的真实下载地址。该工具支持带密码和不带密码的蓝奏云分享链接,并且会返回文件的名称和大小等信息。

 第三方下载渠道:

​​​​​​蓝奏云直链获取在线解析网站源码-资源吧

本文具有以下优势

详细的代码注释和解释

文章不仅提供了完整的代码实现,还逐步解释了每一部分代码的作用和实现逻辑,帮助读者更好地理解脚本的工作原理。

2. 清晰的逻辑结构

文章按步骤详细介绍了脚本的工作流程,从初始化和参数处理到链接有效性检查、文件信息提取、带密码链接处理、获取最终下载链接,再到辅助函数的详细讲解。每一步骤都有清晰的描述和相应的代码示例,使读者能够轻松跟随和理解。

3. 提供了实用的工具

文章提供了一个实用的蓝奏云直链获取工具,这对于需要批量下载或自动化下载蓝奏云资源的用户来说非常有用。

4. 包含了错误处理

脚本中包含了多种错误处理机制,如检测链接有效性、处理带密码的链接、以及处理可能的解析错误,增加了脚本的健壮性。

5. 实用的辅助函数

文章中的辅助函数(如随机IP生成、HTTP请求处理)非常实用,提供了一个较为全面的解决方案,读者可以将这些函数用于其他类似的需求。

6. 突出了安全性

文章强调了使用随机IP地址来模拟请求,这有助于避免因频繁请求同一IP地址而导致的封禁问题,提高了工具的安全性和隐蔽性。

7. JSON输出格式

脚本使用JSON格式输出结果,方便与其他系统进行集成和数据交换,提高了实用性和扩展性。

8. 直观的示例代码

文章提供的示例代码直观易懂,读者可以直接复制粘贴使用,并根据自己的需求进行修改和扩展。

9. 关注用户体验

文章提供了用户体验良好的工具,最终的下载链接可以直接跳转到下载页面,简化了用户的操作步骤,提高了用户的下载体验。

核心代码解析

以下是这个工具的核心PHP代码,我们会逐步分析每个部分的功能和实现细节。

/**
 * @package Lanzou
 * @link https://www.dkewl.com
 */
header('Access-Control-Allow-Origin:*');
header('Content-Type:application/json; charset=utf-8');

// 默认UA
$UserAgent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36';

// 获取输入参数
$url = isset($_GET['url']) ? $_GET['url'] : "";
$pwd = isset($_GET['pwd']) ? $_GET['pwd'] : "";
$type = isset($_GET['type']) ? $_GET['type'] : "";

// 判断传入链接参数是否为空
if (empty($url)) {
    die(json_encode(array('code' => 400, 'msg' => '请输入URL'), JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
}

// 处理蓝奏云链接
$url = 'https://www.lanzoue.com/'.explode('.com/',$url)[1];
$softInfo = MloocCurlGet($url);

// 判断文件链接是否失效
if (strstr($softInfo, "文件取消分享了") != false) {
    die(json_encode(array('code' => 400, 'msg' => '文件取消分享了'), JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
}

// 取文件名称、大小
preg_match('~style="font-size: 30px;text-align: center;padding: 56px 0px 20px 0px;">(.*?)</div>~', $softInfo, $softName);
if(!isset($softName[1])) {
    preg_match('~<div class="n_box_3fn".*?>(.*?)</div>~', $softInfo, $softName);
}
preg_match('~<div class="n_filesize".*?>大小:(.*?)</div>~', $softInfo, $softFilesize);
if(!isset($softFilesize[1])) {
    preg_match('~<span class="p7">文件大小:</span>(.*?)<br>~', $softInfo, $softFilesize);
}
if(!isset($softName[1])) {
    preg_match('~var filename = \'(.*?)\';~', $softInfo, $softName);
}
if(!isset($softName[1])) {
    preg_match('~div class="b"><span>(.*?)</span></div>~', $softInfo, $softName);
}

// 处理带密码的链接
if(strstr($softInfo, "function down_p(){") != false) {
    if(empty($pwd)) {
        die(json_encode(array('code' => 400, 'msg' => '请输入分享密码'), JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
    }
    preg_match_all("~action=(.*?)&sign=(.*?)&p='\+(.*?),~", $softInfo, $segment);
    $post_data = array(
        "action" => 'downprocess',
        "sign" => $segment[2][1],
        "p" => $pwd
    );
    $softInfo = MloocCurlPost($post_data, "https://www.lanzoue.com/ajaxm.php", $url);
    $softName[1] = json_decode($softInfo,JSON_UNESCAPED_UNICODE)['inf'];
} else {
    // 处理不带密码的链接
    preg_match("~\n<iframe.*?name=\"[\s\S]*?\"\ssrc=\"\/(.*?)\"~", $softInfo, $link);
    if(empty($link[1])) {
        preg_match("~<iframe.*?name=\"[\s\S]*?\"\ssrc=\"\/(.*?)\"~", $softInfo, $link);
    }
    $ifurl = "https://www.lanzoue.com/" . $link[1];
    $softInfo = MloocCurlGet($ifurl);
    preg_match_all("~sign':'(.*?)'~", $softInfo, $segment);
    $post_data = array(
        "action" => 'downprocess',
        "signs"=>"?ctdf",
        "sign" => $segment[1][0],
    );
    $softInfo = MloocCurlPost($post_data, "https://www.lanzoue.com/ajaxm.php", $ifurl);
}

// 解析最终直链地址
$softInfo = json_decode($softInfo, true);
if ($softInfo['zt'] != 1) {
    die(json_encode(array('code' => 400, 'msg' => $softInfo['inf']), JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
}

$downUrl1 = $softInfo['dom'] . '/file/' . $softInfo['url'];
$downUrl2 = MloocCurlHead($downUrl1,"https://developer.lanzoug.com",$UserAgent,"down_ip=1; expires=Sat, 16-Nov-2019 11:42:54 GMT; path=/; domain=.baidupan.com");

// 判断最终链接是否获取成功
if($downUrl2 == "") {
    $downUrl = $downUrl1;
} else {
    $downUrl = $downUrl2;
}

// 返回结果
if ($type != "down") {
    die(json_encode(array('code' => 200, 'msg' => '解析成功', 'name' => isset($softName[1]) ? $softName[1] : "", 'filesize' => isset($softFilesize[1]) ? $softFilesize[1] : "", 'downUrl' => $downUrl), JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
} else {
    header("Location:$downUrl");
    die;
}

// 获取下载链接函数
function MloocCurlGetDownUrl($url) {
    $header = get_headers($url,1);
    if(isset($header['Location'])) {
        return $header['Location'];
    }
    return "";
}

// CURL函数
function MloocCurlGet($url = '', $UserAgent = '') {
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
    if ($UserAgent != "") {
        curl_setopt($curl, CURLOPT_USERAGENT, $UserAgent);
    }
    curl_setopt($curl, CURLOPT_HTTPHEADER, array('X-FORWARDED-FOR:'.Rand_IP(), 'CLIENT-IP:'.Rand_IP()));
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $response = curl_exec($curl);
    curl_close($curl);
    return $response;
}

// POST函数
function MloocCurlPost($post_data = '', $url = '', $ifurl = '', $UserAgent = '') {
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_USERAGENT, $UserAgent);
    if ($ifurl != '') {
        curl_setopt($curl, CURLOPT_REFERER, $ifurl);
    }
    curl_setopt($curl, CURLOPT_HTTPHEADER, array('X-FORWARDED-FOR:'.Rand_IP(), 'CLIENT-IP:'.Rand_IP()));
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_POST, 1);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
    $response = curl_exec($curl);
    curl_close($curl);
    return $response;
}

// 直链解析函数
function MloocCurlHead($url,$guise,$UserAgent,$cookie) {
    $headers = array(
        'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
        'Accept-Encoding: gzip, deflate',
        'Accept-Language: zh-CN,zh;q=0.9',
        'Cache-Control: no-cache',
        'Connection: keep-alive',
        'Pragma: no-cache',
        'Upgrade-Insecure-Requests: 1',
        'User-Agent: '.$UserAgent
    );
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_HTTPHEADER,$headers);
    curl_setopt($curl, CURLOPT_REFERER, $guise);
    curl_setopt($curl, CURLOPT_COOKIE , $cookie);
    curl_setopt($curl, CURLOPT_USERAGENT, $UserAgent);
    curl_setopt($curl, CURLOPT_NOBODY, 0);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLINFO_HEADER_OUT, TRUE);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_TIMEOUT, 10);
    $data = curl_exec($curl);
    $url = curl_getinfo($curl);
    curl_close($curl);
    return $url["redirect_url"];
}

// 随机IP函数
function Rand_IP() {
    $ip2id = round(rand(600000, 2550000) / 10000);
    $ip3id = round(rand(600000, 2550000) / 10000);
    $ip4id = round(rand(600000, 2550000) / 10000);
    $arr_1 = array("218","218","66","66","218","218","60","60","202","204","66","66","66","59","61","60","222","221","66","59","60","60","66","218","218","62","63","64","66","66","122","211");
    $randarr= mt_rand(0,count($arr_1)-1);
    $ip1id = $arr_1[$randarr];
    return $ip1id.".".$ip2id.".".$ip3id.".".$ip4id;
}

1. 初始化和输入参数处理

首先,脚本设置了跨域头和返回类型为JSON,并初始化了一个默认的用户代理字符串。然后,通过$_GET获取传入的urlpwdtype参数。

header('Access-Control-Allow-Origin:*');
header('Content-Type:application/json; charset=utf-8');
$UserAgent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36';
$url = isset($_GET['url']) ? $_GET['url'] : "";
$pwd = isset($_GET['pwd']) ? $_GET['pwd'] : "";
$type = isset($_GET['type']) ? $_GET['type'] : "";

2. 链接有效性检查和处理

如果没有提供URL,脚本将返回错误信息。接着,将蓝奏云链接转换为标准格式,并使用MloocCurlGet函数获取页面内容。

if (empty($url)) {
    die(json_encode(array('code' => 400, 'msg' => '请输入URL'), JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
}
$url = 'https://www.lanzoue.com/'.explode('.com/',$url)[1];
$softInfo = MloocCurlGet($url);

3. 提取文件信息

使用正则表达式从页面内容中提取文件名称和大小。如果文件已经取消分享,将返回错误信息。

if (strstr($softInfo, "文件取消分享了") != false) {
    die(json_encode(array('code' => 400, 'msg' => '文件取消分享了'), JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
}
preg_match('~style="font-size: 30px;text-align: center;padding: 56px 0px 20px 0px;">(.*?)</div>~', $softInfo, $softName);
if(!isset($softName[1])) {
    preg_match('~<div class="n_box_3fn".*?>(.*?)</div>~', $softInfo, $softName);
}
preg_match('~<div class="n_filesize".*?>大小:(.*?)</div>~', $softInfo, $softFilesize);
if(!isset($softFilesize[1])) {
    preg_match('~<span class="p7">文件大小:</span>(.*?)<br>~', $softInfo, $softFilesize);
}
if(!isset($softName[1])) {
    preg_match('~var filename = \'(.*?)\';~', $softInfo, $softName);
}
if(!isset($softName[1])) {
    preg_match('~div class="b"><span>(.*?)</span></div>~', $softInfo, $softName);
}

4. 带密码链接处理

如果文件需要密码访问,脚本将根据页面内容构造POST请求获取下载链接。

if(strstr($softInfo, "function down_p(){") != false) {
    if(empty($pwd)) {
        die(json_encode(array('code' => 400, 'msg' => '请输入分享密码'), JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
    }
    preg_match_all("~action=(.*?)&sign=(.*?)&p='\+(.*?),~", $softInfo, $segment);
    $post_data = array(
        "action" => 'downprocess',
        "sign" => $segment[2][1],
        "p" => $pwd
    );
    $softInfo = MloocCurlPost($post_data, "https://www.lanzoue.com/ajaxm.php", $url);
    $softName[1] = json_decode($softInfo,JSON_UNESCAPED_UNICODE)['inf'];
} else {
    // 不带密码的链接处理
    preg_match("~\n<iframe.*?name=\"[\s\S]*?\"\ssrc=\"\/(.*?)\"~", $softInfo, $link);
    if(empty($link[1])) {
        preg_match("~<iframe.*?name=\"[\s\S]*?\"\ssrc=\"\/(.*?)\"~", $softInfo, $link);
    }
    $ifurl = "https://www.lanzoue.com/" . $link[1];
    $softInfo = MloocCurlGet($ifurl);
    preg_match_all("~sign':'(.*?)'~", $softInfo, $segment);
    $post_data = array(
        "action" => 'downprocess',
        "signs"=>"?ctdf",
        "sign" => $segment[1][0],
    );
    $softInfo = MloocCurlPost($post_data, "https://www.lanzoue.com/ajaxm.php", $ifurl);
}

5. 获取最终下载链接

脚本将解析蓝奏云返回的数据,获取最终的下载链接,并判断是否直接下载或返回下载信息。

$softInfo = json_decode($softInfo, true);
if ($softInfo['zt'] != 1) {
    die(json_encode(array('code' => 400, 'msg' => $softInfo['inf']), JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
}

$downUrl1 = $softInfo['dom'] . '/file/' . $softInfo['url'];
$downUrl2 = MloocCurlHead($downUrl1,"https://developer.lanzoug.com",$UserAgent,"down_ip=1; expires=Sat, 16-Nov-2019 11:42:54 GMT; path=/; domain=.baidupan.com");

if($downUrl2 == "") {
    $downUrl = $downUrl1;
} else {
    $downUrl = $downUrl2;
}

if ($type != "down") {
    die(json_encode(array('code' => 200, 'msg' => '解析成功', 'name' => isset($softName[1]) ? $softName[1] : "", 'filesize' => isset($softFilesize[1]) ? $softFilesize[1] : "", 'downUrl' => $downUrl), JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
} else {
    header("Location:$downUrl");
    die;
}

辅助函数

脚本中还包含一些辅助函数,用于处理HTTP请求和生成随机IP地址。

// 获取下载链接函数
function MloocCurlGetDownUrl($url) {
    $header = get_headers($url,1);
    if(isset($header['Location'])) {
        return $header['Location'];
    }
    return "";
}

// CURL函数
function MloocCurlGet($url = '', $UserAgent = '') {
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
    if ($UserAgent != "") {
        curl_setopt($curl, CURLOPT_USERAGENT, $UserAgent);
    }
    curl_setopt($curl, CURLOPT_HTTPHEADER, array('X-FORWARDED-FOR:'.Rand_IP(), 'CLIENT-IP:'.Rand_IP()));
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $response = curl_exec($curl);
    curl_close($curl);
    return $response;
}

// POST函数
function MloocCurlPost($post_data = '', $url = '', $ifurl = '', $UserAgent = '') {
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_USERAGENT, $UserAgent);
    if ($ifurl != '') {
        curl_setopt($curl, CURLOPT_REFERER, $ifurl);
    }
    curl_setopt($curl, CURLOPT_HTTPHEADER, array('X-FORWARDED-FOR:'.Rand_IP(), 'CLIENT-IP:'.Rand_IP()));
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_POST, 1);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
    $response = curl_exec($curl);
    curl_close($curl);
    return $response;
}

// 直链解析函数
function MloocCurlHead($url,$guise,$UserAgent,$cookie) {
    $headers = array(
        'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
        'Accept-Encoding: gzip, deflate',
        'Accept-Language: zh-CN,zh;q=0.9',
        'Cache-Control: no-cache',
        'Connection: keep-alive',
        'Pragma: no-cache',
        'Upgrade-Insecure-Requests: 1',
        'User-Agent: '.$UserAgent
    );
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_HTTPHEADER,$headers);
    curl_setopt($curl, CURLOPT_REFERER, $guise);
    curl_setopt($curl, CURLOPT_COOKIE , $cookie);
    curl_setopt($curl, CURLOPT_USERAGENT, $UserAgent);
    curl_setopt($curl, CURLOPT_NOBODY, 0);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLINFO_HEADER_OUT, TRUE);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_TIMEOUT, 10);
    $data = curl_exec($curl);
    $url = curl_getinfo($curl);
    curl_close($curl);
    return $url["redirect_url"];
}

// 随机IP函数
function Rand_IP() {
    $ip2id = round(rand(600000, 2550000) / 10000);
    $ip3id = round(rand(600000, 2550000) / 10000);
    $ip4id = round(rand(600000, 2550000) / 10000);
    $arr_1 = array("218","218","66","66","218","218","60","60","202","204","66","66","66","59","61","60","222","221","66","59","60","60","66","218","218","62","63","64","66","66","122","211");
    $randarr= mt_rand(0,count($arr_1)-1);
    $ip1id = $arr_1[$randarr];
    return $ip1id.".".$ip2id.".".$ip3id.".".$ip4id;
}

总结

这个蓝奏云直链获取工具通过解析页面内容,提取文件信息,并最终获取下载链接。脚本还包含了一些辅助函数来处理HTTP请求和生成随机IP地址,以增加请求的可靠性。通过这个工具,用户可以方便地获取蓝奏云的直链下载地址,提升下载体验。

悦读

道可道,非常道;名可名,非常名。 无名,天地之始,有名,万物之母。 故常无欲,以观其妙,常有欲,以观其徼。 此两者,同出而异名,同谓之玄,玄之又玄,众妙之门。

;