php Youtube 从 live_stream 获取视频 ID

php Youtube Get video id from live_stream

YouTube 如何使用 php 从 live_stream 获取视频 ID。我用这个 url

 https://www.youtube.com/embed/live_stream?channel=UCmyKnNRH0wH-r8I-ceP-dsg

我尝试了这段代码,但return什么都没有

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("#(?<=v=)[a-zA-Z0-9-]+(?=&)|(?<=v\/)[^&\n]+(?=\?)|(?<=v=)[^&\n]+|(?<=youtu.be/)[^&\n]+#", $link, $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;
}

您正在使用变量 $link 而不是 $data,请尝试将其更改为:

if(preg_match("#(?<=v=)[a-zA-Z0-9-]+(?=&)|(?<=v\/)[^&\n]+(?=\?)|(?<=v=)[^&\n]+|(?<=youtu.be/)[^&\n]+#", $data, $matches))
        $videoId = substr($matches[0], 0, -2);