San*_*r S -1 php youtube youtube-api
我有你管视频链接:例如:http://www.youtube.com/watch?v = EF5FnKTsIbc&feature = youtube_gdata_player我想用php获取这个链接的描述和视频标题和图片,怎么做?
这是代码,我测试它并且工作得很好.感谢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;
Run Code Online (Sandbox Code Playgroud)
// 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;
}
Run Code Online (Sandbox Code Playgroud)
由Lothar通知这里是Json链接,您可以使用json_decode轻松解析此JSON数据
注意:json_decode函数适用于PHP> = 5.2.0
http://gdata.youtube.com/feeds/api/videos/[VIDEO_ID]?v=2&alt=jsonc
Run Code Online (Sandbox Code Playgroud)
例如
http://gdata.youtube.com/feeds/api/videos/37l11UzbvvA?v=2&alt=jsonc
Run Code Online (Sandbox Code Playgroud)