首页 > 解决方案 > 如何从 youtube 频道 html 获取实时视频 ID

问题描述

如何使用简单的 HTML dom 解析器或任何其他方法而不是 YouTube api 从 YouTube 频道获取实时视频 ID?

https://www.youtube.com/embed/live_stream?channel=UC8Z-VjXBtDJTvq6aqkIskPg&autoplay=1

因为 YouTube api 无法获取实时视频 ID。

标签: phphtmlhtml-parsing

解决方案


最后我资助答案

 function getvideourl($chid)
 {
      $videoId = null;

// Fetch the livestream page
if($data = file_get_contents('https://www.youtube.com/embed/live_stream? 
channel='.$chid))
{
    // Find the video ID in there
    if(preg_match('/\'VIDEO_ID\': \"(.*?)\"/', $data, $matches))
        $videoId = $matches[1];

    else
        $videoId ="";
}
else
    throw new Exception('Couldn\'t fetch data');

$video_url = "https://www.youtube.com/embed/".$videoId;


return $video_url;
}

推荐阅读