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

时间:2012-04-10 16:55:47

标签: php youtube youtube-api

我有你管视频链接:             例如:http://www.youtube.com/watch V = EF5FnKTsIbc&安培;特征= youtube_gdata_player             我想用php获取这个链接的描述和视频标题和图像,怎么做?

2 个答案:

答案 0 :(得分:7)

这是代码,我测试了它并且效果很好。 感谢IBM Developer network

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


USAGE

// 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;      
} 

Lothar 通知这是Json链接, 您可以使用json_decode

轻松解析此JSON数据
  

注意:json_decode函数适用于PHP&gt; = 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

答案 1 :(得分:2)

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

<?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,下次遇到任何问题时,请提出您已经完成的事情。这里的人肯定会帮助你。