-1

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

4

2 回答 2

7

这是代码,我对其进行了测试并且效果很好。感谢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;      
} 

正如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
于 2012-08-24T05:30:37.817 回答
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 APIs 文档是你的圣经。你会在那里得到你想要的一切。该链接是@ceejayoz 在评论部分提供的。

PS:欢迎来到SO,下次有什么问题,把你已经做过的贴上来。这里的人肯定会帮助你。

于 2012-04-10T17:17:30.477 回答