You are here

public static function MediaInternetYouTubeHandler::validId in Media: YouTube 7.2

Same name and namespace in other branches
  1. 7.3 includes/MediaInternetYouTubeHandler.inc \MediaInternetYouTubeHandler::validId()

Check if a YouTube video ID is valid.

Return value

boolean TRUE if the video ID is valid, or throws a MediaInternetValidationException otherwise.

1 call to MediaInternetYouTubeHandler::validId()
MediaInternetYouTubeHandler::parse in includes/MediaInternetYouTubeHandler.inc

File

includes/MediaInternetYouTubeHandler.inc, line 90
Extends the MediaInternetBaseHandler class to handle YouTube videos.

Class

MediaInternetYouTubeHandler
Implementation of MediaInternetBaseHandler.

Code

public static function validId($id) {
  $uri = file_stream_wrapper_uri_normalize('youtube://v/' . check_plain($id));
  $external_url = file_create_url($uri);
  $oembed_url = url('http://www.youtube.com/oembed', array(
    'query' => array(
      'url' => $external_url,
      'format' => 'json',
    ),
  ));
  $response = drupal_http_request($oembed_url, array(
    'method' => 'HEAD',
  ));
  if ($response->code == 401) {
    throw new MediaInternetValidationException('Embedding has been disabled for this YouTube video.');
  }
  elseif ($response->code != 200) {
    throw new MediaInternetValidationException('The YouTube video ID is invalid or the video was deleted.');
  }
  return TRUE;
}