如何获取YouTube视频详细信息的描述


how to get description of youtube video details

我有你的电子管视频链接: example:http://www.youtube.com/watch?v=EF5FnKTsIbc&feature=youtube_gdata_player 我想使用PHP获取此链接的描述,视频标题和图像,该怎么做?

这是代码,我测试了它并且效果很好。感谢 IBM 开发人员网络

您可以在此处查看整个代码。http://www.ibm.com/developerworks/xml/library/x-youtubeapi/


用法

// For the video >>> youtube.com/watch?v=37l11UzbvvA
// Video id = 37l11UzbvvA;
$vid="37l11UzbvvA";
// set video data feed URL
$feedURL = 'http://gdata.youtube.com/feeds/api/videos/' . $vid;
// read feed into SimpleXML object
$entry = simplexml_load_file($feedURL);
$video = parseVideoEntry($entry);
echo $video->title;

函数> parseVideoEntry()

// function to parse a video <entry>
function parseVideoEntry($entry) {      
  $obj= new stdClass;
  // get author name and feed URL
  $obj->author = $entry->author->name;
  $obj->authorURL = $entry->author->uri;
  // 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 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;      
} 

正如洛塔尔所通知的那样,这里是 json 链接,您可以使用json_decode轻松解析此 JSON 数据

注意:json_decode函数适用于 PHP>=5.2.0

http://gdata.youtube.com/feeds/api/videos/[VIDEO_ID]?v=2&alt=jsonc

例如

http://gdata.youtube.com/feeds/api/videos/37l11UzbvvA?v=2&alt=jsonc

下面的代码是从我的脚本直接复制粘贴的。不确定它是否仍然有效。你可以试一试。

<?php
$id = '38z8TPtT1BE';
$url = "http://gdata.youtube.com/feeds/api/videos/$id?v=2&alt=json";
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  
$output = curl_exec($ch); 
curl_close($ch);  
print_r(json_decode($output));
?>

Youtube API文档是你的圣经。你会在那里得到你想要的一切。该链接是@ceejayoz评论部分中给出的链接。

PS:欢迎来到SO,下次您遇到任何问题时,请提出您已经完成的工作。这里的人肯定会帮助你。