Home > Bookmarks, PhP, Tech > Check if youtube video is still valid – YouTube API

Check if youtube video is still valid – YouTube API

August 6th, 2009 Leave a comment Go to comments

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]);
}
}
?>

youtube invalid video

$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
  1. Xman
    August 19th, 2009 at 22:12 | #1

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

  1. No trackbacks yet.
GoCache - ByREV-Cache v1.0 - live served in : 0.267787 sec (gzip)