在我的一个应用程序中,我需要显示一个YouTube视频。如果用户提交了一个视频,那么我必须检查该视频是否在YouTube上存在。如果可以,那么我必须将视频id保存在我的数据库中,并在网页中生成视频。
有什么方法可以验证YouTube视频吗?
使用此类提取和验证youtube视频。这适用于YT URL,如/embed/,/v/?v=/,youtu.为
class Youtube {
///// Put together by Sugato
////////// $video_id is the youtube video ID /////////////////////////
public $video_id = null;
///////// the Constructer ////////////////////////////////////////
public function __construct($url)
{
if (preg_match('/youtube'.com'/watch'?v=([^'&'?'/]+)/', $url, $id)) {
$this->video_id = $id[1];
} else if (preg_match('/youtube'.com'/embed'/([^'&'?'/]+)/', $url, $id)) {
$this->video_id = $id[1];
} else if (preg_match('/youtube'.com'/v'/([^'&'?'/]+)/', $url, $id)) {
$this->video_id = $id[1];
} else if (preg_match('/youtu'.be'/([^'&'?'/]+)/', $url, $id)) {
$this->video_id = $id[1];
} else {
$this->video_id = NULL;
}
}
/////////// validates if a youtube video actually exists //////////////
function validate()
{
if(empty($this->video_id))
{
return false;
}
else {
$curl = curl_init("http://gdata.youtube.com/feeds/api/videos/" . $this->video_id);
curl_setopt($curl, CURLOPT_HEADER, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_exec($curl);
$request = curl_getinfo($curl);
curl_close($curl);
$result = explode(";", $request["content_type"]);
if($result[0] == "application/atom+xml")
{
return true;
} else {
return false;
}
}
}
}
像这样调用类
$yt = new Youtube($your_video_link_here);
$exist = $yt->validate();
if($exist)
{
echo "Yaaaayyyyyy!";
} else
{
echo "nAAAAyyyy!!!";
}
如果用户直接提交视频,则必须有一个类似数据库的东西,其中包含现有视频的哈希值,以便与之进行比较(例如:SHA校验和),然后检查哈希值是否已经存在。据我所知,谷歌/YouTube没有提供这样的数据库供公众使用,但你可以为用户通过你的服务提交的视频创建自己的数据库。你可以使用其他更先进的技术,但它们需要访问所有现有的视频文件进行分析。。。这是不可用的。
就获取视频URL而言,当你将视频上传到YouTube时,你可以链接到它或将其嵌入网页。