<?php
/**
* 名称:蓝奏云解析接口
* 出处:https://www.yowal.cn/
* 方式:GET
* 参数:url/id, pwd
* 时间:2021年10月09日
*/
error_reporting(0);
date_default_timezone_set('PRC');

header('Access-Control-Allow-Origin: *');
header('Content-type: application/json');


function getSubStr($str, $leftStr, $rightStr){
    $left = strpos($str, $leftStr);
    $right = strpos($str, $rightStr,$left);
    if($left < 0 or $right < $left) return '';
    return substr($str, $left + strlen($leftStr), $right-$left-strlen($leftStr));
}

function curl_get($url, $post = 0, $referer = 0, $cookie = 0, $header = 0, $ua = 0, $nobaody = false, $split = false) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    $httpheader[] = "Accept:*/*";
    $httpheader[] = "Accept-Encoding:gzip,deflate,sdch";
    $httpheader[] = "Accept-Language:zh-CN,zh;q=0.8";
    $httpheader[] = "Connection:close";
    curl_setopt($ch, CURLOPT_HTTPHEADER, $httpheader);
    if ($post) {
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
    }
    if ($header) {
        curl_setopt($ch, CURLOPT_HEADER, TRUE);
    }
    if ($cookie) {
        curl_setopt($ch, CURLOPT_COOKIE, $cookie);
    }
    if ($referer) {
        curl_setopt($ch, CURLOPT_REFERER, $referer);
    }
    if ($ua) {
        curl_setopt($ch, CURLOPT_USERAGENT, $ua);
    } else {
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36');
    }
    if ($nobaody) {
        curl_setopt($ch, CURLOPT_NOBODY, 1);
    }
    curl_setopt($ch, CURLOPT_ENCODING, "gzip");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $ret = curl_exec($ch);
    if ($split) {
        $headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
        $header = substr($ret, 0, $headerSize);
        $body = substr($ret, $headerSize);
        $ret = array();
        $ret['header'] = $header;
        $ret['body'] = $body;
    }
    curl_close($ch);
    return $ret;
}


function curl_get_loction($url){
    $curl = curl_init();
    $headers = array(
        'Connection:keep-alive',
        'Upgrade-Insecure-Requests:1',
        'User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.10 Safari/537.36',
        'Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
        'Accept-Encoding:gzip, deflate, br',
        'Accept-Language:zh-CN,zh;q=0.9'
    );
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_HEADER, true);
    //设置获取的信息以文件流的形式返回,而不是直接输出。
    curl_setopt($curl,CURLOPT_HTTPHEADER,$headers);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 0);
    //#关闭SSL
    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;
}

$id = isset($_GET['id']) ? trim($_GET['id']) : "";
if ($id != "" ) {
    $lanzou = "https://www.lanzoui.com/";
    $url = $lanzou.$id;
} else {
    $url = isset($_GET['url']) ? trim($_GET['url']) : "";
    $lanzou = substr($url, 0, strrpos($url,"/") + 1);
}
if ($url == "") {
    $re_data = array('code'=>201,'msg'=>'链接未输入');
    die(json_encode($re_data,JSON_UNESCAPED_UNICODE));
}

$str = curl_get($url);
if (strpos($str, '输入密码') == false) {
    preg_match_all('|<iframe(.+?)src="(.+?)"(.+?)frameborder="0"|i', $str, $datarr);

    // 这样是为了,部分匹配不到
    preg_match_all("/src=\"(.*)\" /", $datarr[0][0], $datarr);

    $str = curl_get($lanzou.$datarr[1][0]);

    $js = getSubStr($str, 'text/javascript">','success:function(msg)')."})";

    $ajax = getSubStr($js,"ajax(", ")");
    // 把注释的去掉,不被受干扰
    $ajax = str_replace("//data : ", "//注释内容", $ajax);
    $json = getSubStr($ajax,"data : {", "}");
    $url = $lanzou.getSubStr($ajax,"url : '", "',");

    /* 匹配参数 */
    preg_match_all("/var (.*) = '(.*)'/", $str, $datarr);
    for ($i=0; $i < count($datarr[1]); ++$i) {
        $json = str_replace($datarr[1][$i], "'".$datarr[2][$i]."'", $json);
    }

    $json = str_replace(",", "&", $json);
    $json = str_replace("'", "", $json);
    $data = str_replace(":", "=", $json);

    $str = curl_get($url, $data, $lanzou);

    $data = json_decode($str, true);
    $down_url = $data["dom"]."/file/".$data["url"];

    $hearder = curl_get_loction($down_url)."end";

    preg_match('~Location: (.*?)[\s\S]*?end~',$hearder,$real_url);
    $real_url = trim(str_replace(array('Location:','/r/n','/r','/n','end'),'',$real_url[0]));
    $re_data = array('code'=>200,'url'=>$real_url);
    die(json_encode($re_data,JSON_UNESCAPED_UNICODE));
}

// 下面是带密码的解析
$pwd = isset($_GET['pwd']) ? trim($_GET['pwd']) : "";
if ($pwd == "") {
    $re_data = array('code'=>202,'msg'=>'密码未输入');
    die(json_encode($re_data,JSON_UNESCAPED_UNICODE));
}
$js = getSubStr($str, 'text/javascript">','success:function(msg)')."})";
$url = $lanzou.getSubStr($js,"url : '", "',");
$data = getSubStr($js,"data : '", "'+");
$str = curl_get($url,$data.$pwd,$lanzou);
$data = json_decode($str,true);
$down_url = $data["dom"]."/file/".$data["url"];
$hearder = curl_get_loction($down_url)."end";
preg_match('~Location: (.*?)[\s\S]*?end~',$hearder,$real_url);
$real_url = trim(str_replace(array('Location:','/r/n','/r','/n','end'),'',$real_url[0]));
$re_data = array('code'=>200,'url'=>$real_url);
die(json_encode($re_data,JSON_UNESCAPED_UNICODE));
?>

这是一份PHP的蓝奏云解析,支持无密码和带密码的,自动识别有无密码,这份源码理论上是通杀后续蓝奏云的,这是自动分析蓝奏云的js而不是直接匹配,js里的数据,我写完就分享出来了,毕竟蓝奏云还是很好用的。

END
文章标题:蓝奏云解析接口
本文链接:https://yowal.cn/archives/32.html
版权说明:若无注明,本文皆枫岚剥壳 - 一个乐于分享的地方原创,转载请保留文章出处。
作品采用:《署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)》许可协议授权。
最后修改:2021 年 10 月 09 日 12 : 16 AM