Check if youtube video is still valid – YouTube API
fast PHP function to check if youtube video is still valid:
<?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;
}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]);
}
}
?>
$vid posible format/sintax:
- http ://www.youtube.com/watch?v=[ID]&feature=…&…
- http ://www.youtube.com/watch?v=[ID]
- www.youtube.com/watch?v=[ID]
- youtube.com/watch?v=[ID]
- http ://www.youtube.com/v/[ID]
- www.youtube.com/v/[ID]
- youtube.com/v/[ID]
- [ID]
- …. and any valid youtube video url
Categories: Bookmarks, PhP, Tech check, Check yotube, id, PhP, script, valid youtube, youtube id, youtube php










It’s very good idea , thank you very much .