user1041717
user1041717

Reputation: 31

Check if a youtube video is embeddable using this code

function parseVideoEntry($entry) {

  $obj= new stdClass;

  // get nodes in media: namespace for media information
  $media = $entry->children('http://search.yahoo.com/mrss/');
  $obj->title = $media->group->title;
  $obj->description = $media->group->description;

  // get video player URL
  $attrs = $media->group->player->attributes();
  $obj->watchURL = $attrs['url']; 

  // get video thumbnail
  $attrs = $media->group->thumbnail[0]->attributes();
  $obj->thumbnailURL = $attrs['url']; 

  // get <yt:duration> node for video length
  $yt = $media->children('http://gdata.youtube.com/schemas/2007');
  $attrs = $yt->duration->attributes();
  $obj->length = $attrs['seconds']; 

  // get <yt:stats> node for viewer statistics
  $yt = $entry->children('http://gdata.youtube.com/schemas/2007');
  $attrs = $yt->statistics->attributes();
  $obj->viewCount = $attrs['viewCount']; 

  // get <gd:rating> node for video ratings
  $gd = $entry->children('http://schemas.google.com/g/2005'); 
  if ($gd->rating) { 
    $attrs = $gd->rating->attributes();
    $obj->rating = $attrs['average']; 
  } else {
    $obj->rating = 0;         
  }

  // get <gd:comments> node for video comments
  $gd = $entry->children('http://schemas.google.com/g/2005');
  if ($gd->comments->feedLink) { 
    $attrs = $gd->comments->feedLink->attributes();
    $obj->commentsURL = $attrs['href']; 
    $obj->commentsCount = $attrs['countHint']; 
  }

  //Get the author
  $obj->author = $entry->author->name;
  $obj->authorURL = $entry->author->uri;


  // get feed URL for video responses
  $entry->registerXPathNamespace('feed', 'http://www.w3.org/2005/Atom');
  $nodeset = $entry->xpath("feed:link[@rel='http://gdata.youtube.com/schemas/
  2007#video.responses']"); 
  if (count($nodeset) > 0) {
    $obj->responsesURL = $nodeset[0]['href'];      
  }

  // get feed URL for related videos
  $entry->registerXPathNamespace('feed', 'http://www.w3.org/2005/Atom');
  $nodeset = $entry->xpath("feed:link[@rel='http://gdata.youtube.com/schemas/
  2007#video.related']"); 
  if (count($nodeset) > 0) {
    $obj->relatedURL = $nodeset[0]['href'];      
  }

  // return object to caller  
  return $obj;      

}

    $vid = stripslashes($_POST['url']);
    $string = $vid;
    $url = parse_url($string);
    parse_str($url['query']); 

    if(isset($v))
    {

         // set video data feed URL
        $feedURL = 'http://gdata.youtube.com/feeds/api/videos/'. $v;

        // read feed into SimpleXML object
        $entry = simplexml_load_file($feedURL);

        // parse video entry
        $video = parseVideoEntry($entry);


        //These variables include the video information
        $video_title = $video->title;
        $video_lenght =$video->length;

I have tried looking at the API and stuff, but I'm not used to using these Apis so I can't relly solve this, I must say im a designer and not a developer, if someone can help me I'd really appreciate it...

I want to check if a video is TOTALLY embedable, no area restrictions and stuff

Thank you

Upvotes: 0

Views: 1167

Answers (2)

ConnorLaCombe
ConnorLaCombe

Reputation: 320

You can add &format=5 to your YouTube API request, however, you'll still run into videos that throw an embed error. To handle those, just add an event listener to the YouTube player for "onError" then error numbers 150 and 101 will be passed if the video can't be embedded.

Upvotes: 0

Esailija
Esailija

Reputation: 140220

You can check if video embedding is allowed by restricting format to 5 and you can also see if video is available in certain area by setting restriction to ip address like so:

http://gdata.youtube.com/feeds/api/videos?restriction=xxx.xxx.xxx.xxx&format=5&orderby=relevance&q=xxx

This will find videos by the query "xxx", which are playable in the area that the ip address xxx.xxx.xxx.xxx is in and have embedding allowed and sorts them by relevance.

reference for format=5: http://code.google.com/apis/youtube/2.0/reference.html#format

Upvotes: 1

Related Questions