【问题标题】:php Youtube Get video id from live_streamphp Youtube 从 live_stream 获取视频 id
【发布时间】:2021-02-10 00:05:42
【问题描述】:

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;
}

【问题讨论】:

标签: php youtube


【解决方案1】:

您正在使用变量$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);

【讨论】:

    猜你喜欢
    • 2012-04-15
    • 2011-03-24
    • 2020-11-05
    • 2011-11-05
    • 2012-09-08
    • 2014-12-15
    • 1970-01-01
    • 2016-09-28
    • 2017-08-25
    相关资源
    最近更新 更多