如何从YouTube频道HTML获取实时视频ID

时间:2019-09-21 12:12:27

标签: php html html-parsing

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

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

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

1 个答案:

答案 0 :(得分:1)

最后我资助答案

 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;
}
相关问题