Vildulv
2/25/2014 - 2:48 PM

A PHP function to get youtube video title by just passing the video ID

A PHP function to get youtube video title by just passing the video ID

function youtube_title($id) {
  // $id = 'YOUTUBE_ID';
  // returns a single line of XML that contains the video title. Not a giant request.
  $videoTitle = @file_get_contents("https://gdata.youtube.com/feeds/api/videos/{$id}?v=2&fields=title");
  // despite @ suppress, it will be false if it fails
  if ($videoTitle) {
    // look for that title tag and get the insides
    preg_match("/<title>(.+?)<\/title>/is", $videoTitle, $titleOfVideo);
    return $titleOfVideo[1];
  } else {
    return false;
  }
  // usage:
  // $item = youtube_title('zgNJnBKMRNw');
}