YouTube如何使用php从live_stream获取视频ID。我使用这个网址
https://www.youtube.com/embed/live_stream?channel=UCmyKnNRH0wH-r8I-ceP-dsg
我尝试此代码,但未返回任何内容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);
关于php - php Youtube从live_stream获取视频ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64559046/