Check youtube video is still valid

Home / PHP / Check youtube video is still valid

Check youtube video is still valid using youtube’s API

Manytimes youtube user can decide to delete their video.So I would
like to periodically run a script to check that the youtube video is still valid or not.

Suppose your Youtube url is : http://www.youtube.com/v/jc0rnCBCX2c
So here your Youtube video Id is jc0rnCBCX2c

$vid posible format/sintax:

1. http://www.youtube.com/watch?v=[ID]&feature=…&…
2. http://www.youtube.com/watch?v=[ID]
3. www.youtube.com/watch?v=[ID]
4. youtube.com/watch?v=[ID]
5. http ://www.youtube.com/v/[ID]
6. www.youtube.com/v/[ID]
7. youtube.com/v/[ID]
8. [ID]
9. …. and any valid youtube video url

<?php
function checkYoutubeId($id) {
if (!$data = @file_get_contents("http://gdata.youtube.com/feeds/api/videos/".$id)) return false;
if ($data == "Video not found") return false;
return true;
}
?>

http://gdata.youtube.com/feeds/api/videos/jc0rnCBCX2c if it does not exist then you will get a page with “Invalid id” if it exists, will return an XML feed having various info about the video. this way you can check that the video exists.

<?php
function checkYoutube($vid) {
if (strlen($vid) < 12) { return checkYoutubeId($vid); }

	$preg1 = '@www.youtube.com\/watch\?v=(.*?)$@';
	if ( preg_match($preg1, $vid, $match) ) {
		$id = explode("&",$match[1]);   return checkYoutubeId($id[0]);
	}

	$preg1 = '@www.youtube.com\/v\/(.*?)$@';
	if ( preg_match($preg1, $vid, $match) ) {
		$id = explode("&",$match[1]);   return checkYoutubeId($id[0]);
	}
}
?>

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *