Youtube iframe api检查视频是否存在 [英] Youtube iframe api check if video exists

查看:88
本文介绍了Youtube iframe api检查视频是否存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理一个带有文本输入的简单页面,用户可以在其中添加要插入页面的YouTube视频的ID,然后我调用youtube的iframe api来加载视频。但是,如果用户添加了一个不存在的ID,我会得到一个黑屏。

I am working on a simple page that has a text input where the user can add the id of the youtube video they want to insert into the page, then I call youtube's iframe api to load the video. However, if the user adds an id that does not exists I get a black screen.

我希望有人知道在加载之前检查视频是否存在的方法。

I was hoping someone knows a way of checking if the video exists before loading it.

这是我的代码:

$(document).ready(function() {
  $('#loadVid').click(function() {
     var player;
     var urlVid = $('#urlVid').val(); //GET THE VIDEO CODE FROM THE TEXT INPUT

     player = new YT.Player('player', {
          height: '390',
          width: '640',
          videoId: urlVid,
          events: {
            'onReady': onPlayerReady,
            'onStateChange': onPlayerStateChange
          }
     });
  });
});


推荐答案


这里$ videoId包含yuotube网址的视频ID

Here $videoId contain video id of yuotube url



public function checkVideoExists() {
        $headers = get_headers('http://gdata.youtube.com/feeds/api/videos/' . $videoId);
        if (!strpos($headers[0], '200')) {
            echo "The YouTube video you entered does not exist";
            return false;
        }
    }

这篇关于Youtube iframe api检查视频是否存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆