使用此代码检查YouTube视频是否可嵌入

时间:2011-11-13 19:10:42

标签: php api youtube embed

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;

我试过看API和东西,但我不习惯使用这些Apis所以我不能解决这个问题,我必须说我是设计师而不是开发人员,如果有人可以帮助我的话我我真的很感激......

我想检查视频是否完全可嵌入,没有区域限制和内容

谢谢

2 个答案:

答案 0 :(得分:1)

您可以通过将format限制为5来检查是否允许视频嵌入,并且您还可以通过将restriction设置为ip address来查看某个区域是否有视频可用这样:

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

这将通过查询"xxx"查找视频,这些视频可在IP地址xxx.xxx.xxx.xxx所在的区域播放,并允许嵌入并按相关性对其进行排序。

格式= 5的参考:http://code.google.com/apis/youtube/2.0/reference.html#format

答案 1 :(得分:0)

您可以在YouTube API请求中添加&amp; format = 5,但是,您仍会遇到引发嵌入错误的视频。要处理这些问题,只需向YouTube播放器添加一个事件监听器以获取“onError”,如果无法嵌入视频,则会传递错误编号150和101.