优酷剧集获取纯php实现

$webApi =[
    'api' => 'https://list.youku.com/category/page?', //请求列表 demo https://list.youku.com/category/page?c=97&type=show&p=1&s=6&u=2
    'cate' => ['电影'=>96,'电视剧'=>97,'综艺'=>85,'少儿'=>177,'动漫'=>100,'纪录片'=>84,'音乐'=>95],
    'mode' => ['hot' => 4, 'new' => 5, 'all' => 6, 'reviews' => 2],  //热门 最新 默认或者最新更新  好评
    'status'=>  ['finish'=>1,'update'=>2], //更新状态
    'getInfoApi' => 'https://api.youku.com/videos/show.json?client_id=af43ee2709aa59ab&ext=show&video_id=', //获取视频信息 demo https://api.youku.com/videos/show.json?client_id=af43ee2709aa59ab&ext=show&video_id=XNDQwODUyMzM0OA==
    'listinfoApi' => 'https://list.youku.com/show/module?callback=json&tab=showInfo', //获取剧集列表 demo https://list.youku.com/show/module?id=308528&tab=showInfo&cname=电视剧&callback=json
    'relistinfoApi' => 'https://list.youku.com/show/episode?callback=json', //获取剧集列表 demo https://list.youku.com/show/episode?id=308528&stage=reload_41&callback=json
];

$videoId = '5b5de27b671c417380c0';
$cate = '电视剧';
var_dump(_get_video_list($webApi,$videoId,$cate)); die();

function _get_video_list($webApi,$videoId,$cate){
    $getChildApi =  $webApi['listinfoApi'].'&id='.$videoId.'&cname='.$cate;
    $listInfo = file_get_contents($getChildApi);
    $listInfo = json_decode(winJsonToJson($listInfo), true);
    if (!is_array($listInfo) && empty($listInfo['html'])) {
       return [];
    }
    $children = [];
    $list = _html_to_list($listInfo['html']);
    $children = array_merge($children,$list['children_list']);
    unset($listInfo);
    if (isset($list['album_tab_list']) && !empty($list['album_tab_list'])){
        foreach ($list['album_tab_list'] as $album_tab){
            if (in_array($album_tab,$list['album_tab_list_finish'])){
                continue;
            }
            $getChildApi =  $webApi['relistinfoApi'].'&id='.$videoId.'&stage='.$album_tab;
            $listInfo = file_get_contents($getChildApi);
            $listInfo = json_decode(winJsonToJson($listInfo), true);
            if (!is_array($listInfo) && empty($listInfo['html'])) {
              continue;
            }
            $relist = _html_to_list($listInfo['html'],true);
            $children = array_merge($children,$relist['children_list']);
            unset($relist);
        }
    }
    return $children;
}

function _html_to_list($html,$reload=false){
    $result = [
        'children_list' =>[],
        'album_tab_list_finish' => []
    ];
    $hackEncoding = '';
    $dom = new DOMDocument();
    @$dom->loadHTML($hackEncoding .$html);//$a是上面得到的一些a标签
    $url = new DOMXPath($dom);
    if (!$reload){
        $result['album_tab_list'] = [];
        $data = $url->evaluate('//ul[contains(@class, "p-tab-pills")]');
        foreach ($data as $val) {
            $item = $val->childNodes;
            foreach ($item as $v) {
                $result['album_tab_list'][] = trim($v->getAttribute('data-id'));
            }
        }
        $data = $url->evaluate('//div[@class="p-panels"]/div');
    }else{
        $data = $url->evaluate('//div[@class="p-panel"]');
    }
    foreach ($data as $val) {
        $item = $val->childNodes;
        $album_tab_list_finish[] = trim($val->getAttribute('id'));
        foreach ($item as $v) {
            foreach ($v->childNodes as $child){
                if ($child->getElementsByTagName('i')->length >0){
                    $icon_labels =  $child->getElementsByTagName('i')->item(0)->getAttribute('class');
                }else{
                    $icon_labels = '';
                }
                if (!empty($icon_labels)){
                    $icon_labels = explode(' ',$icon_labels);
                }
                $result['children_list'][] = [
                    'title' => $child->textContent,
                    'url' => trimUrl('https:'.$child->getElementsByTagName('a')->item(0)->getAttribute('href')),
                    'label_icon' => $icon_labels
                ];
            }
        }
    }
    return $result;
}

function winJsonToJson($string){
    $string = substr($string,strpos($string,'(')+1);
    $string = substr($string,0,stripos($string,')'));
    return $string;
}

function trimUrl($url){
    $url = trimStr($url);
    $pattern="#(http|https)://(.*\.)?.*\..*#i";
    if(!preg_match($pattern,$url)){
        return false;
    }
    $arr = parse_url($url);
    if (empty($arr)){
        return false;
    }
    if ($arr['scheme'] !== 'https' || !isset($arr['scheme'])){
        $arr['scheme'] = 'https';
    }
    return $arr['scheme'].'://'.$arr['host'].$arr['path'];
}
function trimStr($string){
    $string = strip_tags($string);
    return trim(str_replace(array("'",'"'," ","\t","\n","\r","\r\n"), "", $string));
}

版权声明:
作者:超级管理员
链接: https://apecloud.ltd/article/detail.html?id=62
来源:猿码云个人技术站
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
打赏
/static/admin/img/weixin.jpg/static/admin/img/zfb.jpg
<<上一篇>
composer爆错:zlib_decode():data error
下一篇>>