现在的位置: 首页 > 综合 > 正文

使用 PHP 的 SimpleXML 扩展处理 YouTube 数据并将其集成到 PHP 应用程序中

2013年06月29日 ⁄ 综合 ⁄ 共 6182字 ⁄ 字号 评论关闭

YouTube 视频共享站点允许 Web 开发人员通过其基于 REST 的开发人员 API 访问公共内容。PHP 的 SimpleXML 扩展非常适合处理由这种 API 生成的 XML 提要,并使用它们构建定制的 PHP 应用程序。本文将向您介绍 YouTube Data API,演示如何使用它浏览用户生成的视频内容,访问视频元数据、评论和回复,并执行关键字搜索。

简介

常用缩写词
  • API:应用程序编程接口(application programming interface)
  • HTTP:超文本传输协议(Hypertext Transfer Protocol)
  • PEAR:PHP 扩展和应用库(PHP Extension and Application Repository)
  • PHP:超文本预处理语言(PHP Hypertext Preprocessor)
  • REST:具象状态传输(Representational state transfer)
  • RSS:真正简单的连锁(Really Simple Syndication
  • URL:统一资源定位符(Uniform Resource Locator
  • XML:可扩展标记语言(Extensible Markup Language)

谈 到在线视频共享,目前最流行的站点非 YouTube 莫属,该站点每天会增加几十亿个页面视图和数十万个视频文件。它不仅仅提供家庭录像:如今,YouTube 还提供音乐节目录像、电视节目剪辑、即将上映的影片片花、动漫剪辑等大量内容。它还允许用户使用关键字为视频添加标记,在任何时刻都可以观看最受欢迎的视 频文件。

YouTube 的一个出色特性就是它的 YouTube Data API,该特性允许开发人员通过基于 REST 的 API 访问和搜索 YouTube 视频数据,并将数据集成到自己的支持 XML 的应用程序中。这个过程实现起来并不困难 — 编写应用程序级别的代码发送 REST 请求、解析和解码响应,然后将结果数据集成到应用程序接口。如果使用 PHP,那么您可以通过 YouTube 的 PHP Client Library 执行这些任务,也可以手动解析 REST 请求的 XML 响应。

本文将讨论后一种方 法,展示如何通过 YouTube API 访问公共内容并使用 SimpleXML 将这些内容集成到您的 PHP 应用程序中。本文提供的示例将检索特定类别中的视频清单;根据关键字搜索视频;检索视频元数据,包括缩略图和统计信息;并访问用户个人信息。



回页首

了解 YouTube 数据格式

在 深入研究 PHP 代码之前,首先简单介绍一下 YouTube Data API。与所有基于 REST 的服务一样,首先从一个发送到指定资源的 HTTP 请求开始。这个 HTTP 请求包含一个查询,具有一个或多个输入参数;服务器使用 Atom 或 RSS 格式的响应回复该查询,该响应适合在支持 XML 的任何客户机内进行解析。

要了解工作原理,请在您喜爱的 Web 浏览器中访问 URL http://gdata.youtube.com/feeds/api/standardfeeds/most_viewed。这个 REST 方法返回 YouTube 上当前查看次数最多的视频列表。该方法的原始 XML 响应(您可以通过结果页面的源代码查看)包含关于这些视频的详细信息,内容类似 清单 1 所示:

清单 1. YouTube API 生成的示例提要

                        
<?xml version='1.0' encoding='UTF-8'?>
<feed xmlns='http://www.w3.org/2005/Atom'
xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/'
xmlns:gml='http://www.opengis.net/gml' xmlns:georss='http://www.georss.org/georss'
xmlns:media='http://search.yahoo.com/mrss/'
xmlns:yt='http://gdata.youtube.com/schemas/2007'
xmlns:gd='http://schemas.google.com/g/2005'>
<id>http://gdata.youtube.com/feeds/api/standardfeeds/most_viewed</id>
<updated>2008-03-06T14:43:27.000-08:00</updated>
<category scheme='http://schemas.google.com/g/2005#kind'
term='http://gdata.youtube.com/schemas/2007#video'/>
<title type='text'>Most Viewed</title>
<logo>http://www.youtube.com/img/pic_youtubelogo_123x63.gif</logo>
<link rel='alternate' type='text/html' href='http://www.youtube.com/browse?s=mp'/>
<link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml'
href='http://gdata.youtube.com/feeds/api/standardfeeds/most_viewed'/>
<link rel='self' type='application/atom+xml'
href='http://gdata.youtube.com/feeds/api/standardfeeds/most_viewed
?start-index=1&max-results=5'/>
<link rel='next' type='application/atom+xml'
href='http://gdata.youtube.com/feeds/api/standardfeeds/most_viewed
?start-index=6&max-results=5'/>
<author>
<name>YouTube</name>
<uri>http://www.youtube.com/</uri>
</author>
<generator version='beta' uri='http://gdata.youtube.com/'>
YouTube data API</generator>
<openSearch:totalResults>94</openSearch:totalResults>
<openSearch:startIndex>1</openSearch:startIndex>
<openSearch:itemsPerPage>5</openSearch:itemsPerPage>
<entry>
<id>http://gdata.youtube.com/feeds/api/videos/dMH0bHeiRNg</id>
<published>2006-04-06T14:30:53.000-07:00</published>
<updated>2008-03-12T00:22:25.000-07:00</updated>
<category scheme='http://gdata.youtube.com/schemas/2007/keywords.cat'
term='Dancing'/>
<category scheme='http://schemas.google.com/g/2005#kind'
term='http://gdata.youtube.com/schemas/2007#video'/>
<category scheme='http://gdata.youtube.com/schemas/2007/keywords.cat'
term='comedy'/>
<category scheme='http://gdata.youtube.com/schemas/2007/categories.cat'
term='Comedy' label='Comedy'/>
<title type='text'>Evolution of Dance</title>
<content type='text'>The funniest 6 minutes you will ever see!
Remember how many of these you have done!
Judson Laipply is dancing -
http://www.evolutionofdance.com -
for more info including song list!</content>
<link rel='alternate' type='text/html'
href='http://www.youtube.com/watch?v=dMH0bHeiRNg'/>
<link rel='http://gdata.youtube.com/schemas/2007#video.responses'
type='application/atom+xml'
href='http://gdata.youtube.com/feeds/api/videos/dMH0bHeiRNg/responses'/>
<link rel='http://gdata.youtube.com/schemas/2007#video.related'
type='application/atom+xml'
href='http://gdata.youtube.com/feeds/api/videos/dMH0bHeiRNg/related'/>
<link rel='self' type='application/atom+xml'
href='http://gdata.youtube.com/feeds/api/standardfeeds/most_viewed/dMH0bHeiRNg'/>
<author>
<name>judsonlaipply</name>
<uri>http://gdata.youtube.com/feeds/api/users/judsonlaipply</uri>
</author>
<media:group>
<media:title type='plain'>Evolution of Dance</media:title>
<media:description type='plain'>The funniest 6 minutes you will ever see!
Remember how many of these you have done!
Judson Laipply is dancing -
http://www.evolutionofdance.com -
for more info including song list!</media:description>
<media:keywords>comedy, Dancing</media:keywords>
<yt:duration seconds='360'/>
<media:category label='Comedy'
scheme='http://gdata.youtube.com/schemas/2007/categories.cat'>Comedy
</media:category>
<media:content
url='http://www.youtube.com/v/dMH0bHeiRNg' type='application/x-shockwave-flash'
medium='video' isDefault='true' expression='full' duration='360' yt:format='5'/>
<media:content
url='rtsp://rtsp2.youtube.com/ChoLENy73wIaEQnYRKJ3bPTBdBMYDSANFEgGDA==
/0/0/0/video.3gp' type='video/3gpp' medium='video' expression='full' duration='360'
yt:format='1'/>
<media:content
url='rtsp://rtsp2.youtube.com/ChoLENy73wIaEQnYRKJ3bPTBdBMYESARFEgGDA==
/0/0/0/video.3gp' type='video/3gpp' medium='video' expression='full' duration='360'
yt:format='6'/>
<media:player url='http://www.youtube.com/watch?v=dMH0bHeiRNg'/>
<media:thumbnail
url='http://img.youtube.com/vi/dMH0bHeiRNg/2.jpg' height='97' width='130'
time='00:03:00'/>
<media:thumbnail
url='http://img.youtube.com/vi/dMH0bHeiRNg/1.jpg' height='97' width='130'
time='00:01:30'/>
<media:thumbnail
url='http://img.youtube.com/vi/dMH0bHeiRNg/3.jpg' height='97' width='130'
time='00:04:30'/>
<media:thumbnail
url='http://img.youtube.com/vi/dMH0bHeiRNg/0.jpg' height='240' width='320'
time='00:03:00'/>
</media:group>
<yt:statistics viewCount='78060679' favoriteCount='400468'/>
<gd:rating min='1' max='5' numRaters='276123' average='4.65'/>
<gd:comments>
<gd:feedLink href='http://gdata.youtube.com/feeds/api/videos/dMH0bHeiRNg
/comments' countHint='124130'/>
</gd:comments>
</entry>
<entry>
<id>http://gdata.youtube.com/feeds/api/videos/cQ25-glGRzI</id>
<published>2007-02-27T15:08:01.000-08:00</published>
<updated>2008-03-12T00:46:25.000-07:00</updated>
...
</entry>
</feed>

本文转自:IBM developerWorks 中国

更多内容请点击此处查看全文



抱歉!评论已关闭.