php - php Youtube从live_stream获取视频ID

标签 php youtube

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/

相关文章:

php - Magento 与 IWD Onepage Checkout 发生 fatal error

javascript - 可以使用 Popcorn.js 为 YouTube 视频添加字幕吗?

jquery - 使用 youtube api 通过 jQuery 获取 youtube 视频的标题

javascript - 有人可以解释极小极大井字游戏算法吗

javascript - AJAX 响应中缺少值

PHP 在 require_once 上失败

php - 无法将字符串偏移量用作数组 - 我需要在 foreach 中反序列化吗?

php - 使用 zmq 和 composer 安装 Ratchet 库

html - JSON - 从 YouTube 播放列表中获取超过 25 个视频

youtube - 验证 YouTube channel 所有权