【问题标题】:how to get description of youtube video details [closed]如何获取 youtube 视频详细信息的描述 [关闭]
【发布时间】:2012-04-10 16:55:47
【问题描述】:

我有你的视频链接: 示例:http://www.youtube.com/watch?v=EF5FnKTsIbc&feature=youtube_gdata_player 我想用php获取这个链接的描述和视频标题和图片,怎么做?

【问题讨论】:

标签: php youtube youtube-api


【解决方案1】:

这是代码,我对其进行了测试,效果很好。 感谢IBM Developer network

您可以在此处查看整个代码。 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;      
} 

根据 Lothar 的通知,这是 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

【讨论】:

【解决方案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 在 cmets 部分提供的。

PS:欢迎来到 SO,下次遇到任何问题,把你已经做过的事贴上来。这里的人一定会帮助你的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-01-10
    • 1970-01-01
    • 2014-04-20
    • 2019-03-08
    • 1970-01-01
    • 1970-01-01
    • 2015-08-23
    • 2016-05-23
    相关资源
    最近更新 更多