【发布时间】:2014-02-13 03:32:19
【问题描述】:
我正在尝试优化我为解析 YouTube 个人资料而编写的一个小 php 应用程序。输入 YouTube 用户名,应用程序通过解析帐户配置文件的 gdata 查询返回的 XML,返回上传视频数量、收藏夹、订阅者等的简单列表。 例如这个 XML:
<?xml version="1.0" encoding="UTF-8" ?>
<entry xmlns="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" xmlns:gd="http://schemas.google.com/g/2005" xmlns:yt="http://gdata.youtube.com/schemas/2007" gd:etag="W/"C0EHSX47eCp7I2A9Wh5aEU4."">
<id>tag:youtube.com,2008:user:H5m_qmnr3dHOO8x7m7dtvw</id>
<published>2006-06-15T22:59:11.000Z</published>
<updated>2014-01-07T21:53:58.000Z</updated>
<category scheme="http://schemas.google.com/g/2005#kind" term="http://gdata.youtube.com/schemas/2007#userProfile" />
<category scheme="http://gdata.youtube.com/schemas/2007/channeltypes.cat" term="DIRECTOR" />
<title>epontius</title>
<summary>channel page of epontius</summary>
<link rel="alternate" type="text/html" href="https://www.youtube.com/channel/UCH5m_qmnr3dHOO8x7m7dtvw" />
<link rel="self" type="application/atom+xml" href="https://gdata.youtube.com/feeds/api/users/H5m_qmnr3dHOO8x7m7dtvw?v=2" />
<author>
<name>epontius</name>
<uri>https://gdata.youtube.com/feeds/api/users/epontius</uri>
<yt:userId>H5m_qmnr3dHOO8x7m7dtvw</yt:userId>
</author>
<yt:channelId>UCH5m_qmnr3dHOO8x7m7dtvw</yt:channelId>
<gd:feedLink rel="http://gdata.youtube.com/schemas/2007#user.subscriptions" href="https://gdata.youtube.com/feeds/api/users/epontius/subscriptions?v=2" countHint="161" />
<gd:feedLink rel="http://gdata.youtube.com/schemas/2007#user.liveevent" href="https://gdata.youtube.com/feeds/api/users/epontius/live/events?v=2" countHint="0" />
<gd:feedLink rel="http://gdata.youtube.com/schemas/2007#user.favorites" href="https://gdata.youtube.com/feeds/api/users/epontius/favorites?v=2" countHint="73" />
<gd:feedLink rel="http://gdata.youtube.com/schemas/2007#user.contacts" href="https://gdata.youtube.com/feeds/api/users/epontius/contacts?v=2" countHint="184" />
<gd:feedLink rel="http://gdata.youtube.com/schemas/2007#user.inbox" href="https://gdata.youtube.com/feeds/api/users/epontius/inbox?v=2" />
<gd:feedLink rel="http://gdata.youtube.com/schemas/2007#user.playlists" href="https://gdata.youtube.com/feeds/api/users/epontius/playlists?v=2" />
<gd:feedLink rel="http://gdata.youtube.com/schemas/2007#user.uploads" href="https://gdata.youtube.com/feeds/api/users/epontius/uploads?v=2" countHint="26" />
<gd:feedLink rel="http://gdata.youtube.com/schemas/2007#user.newsubscriptionvideos" href="https://gdata.youtube.com/feeds/api/users/epontius/newsubscriptionvideos?v=2" />
<gd:feedLink rel="http://gdata.youtube.com/schemas/2007#user.recentactivity" href="https://gdata.youtube.com/feeds/api/users/epontius/events?v=2" />
<yt:googlePlusUserId>105085469892080308187</yt:googlePlusUserId>
<yt:location>US</yt:location>
<yt:statistics lastWebAccess="1970-01-01T00:00:00.000Z" subscriberCount="245" videoWatchCount="0" viewCount="0" totalUploadViews="68815" />
<media:thumbnail url="https://yt3.ggpht.com/-N_Et9Qg1APc/AAAAAAAAAAI/AAAAAAAAAAA/VIuQ_GuzA0Q/s88-c-k-no/photo.jpg" />
<yt:userId>H5m_qmnr3dHOO8x7m7dtvw</yt:userId>
<yt:username display="epontius">epontius</yt:username>
</entry>
该应用程序运行良好,但 YouTube 经常更改某些元素的顺序或添加/删除行中的项目,因此当我访问“countHint”属性时,该应用程序返回不正确的数据。 例如:
$uploadscount = $gd->feedLink[6]->attributes();
$uploads = $uploadscount['countHint'];
echo 'Number of uploads: ' . '<span class="datatext">' . $uploads . '</span>' . '<br />';
在这种情况下会返回 26。但是,如果 feedLink 行的数量或顺序发生变化,我会得到不正确的信息或错误,因为 feedLink 的索引号是硬编码的。 每个 feedLink 似乎都有一个唯一的 rel= 属性,我希望能够使用 xpath 和某种循环(如 foreach)来搜索特定的 rel 值(即 rel = "http://gdata.youtube.com/schemas/2007#user.uploads" )然后能够获取其 countHint 属性值以将其分配给变量或至少获取其节点索引号(即,在上传的情况下为 6),然后访问适当的 countHint 属性。然后对我想要抓取的数据的每个 feedLink 行和属性重复此操作。 这样,如果修改了这些 feedLink 行,它将更加准确和动态。 我只是不知道该怎么做。 feedLink 元素是不同命名空间 (gd) 中的空元素,并且存在多个元素,这使得使用 xpath 让我感到困惑。我不断返回空值并迷路。 任何建议将不胜感激。
好的。多亏了建议,我想我正在取得进展。
foreach ($gd->feedLink as $feedLink) {
$attributes = $feedLink->attributes();
if (strpos($attributes['rel'], '#user.uploads')) {
$uploads = $attributes['countHint'];
}
elseif (strpos($attributes['rel'], '#user.favorites')) {
$favs = $attributes['countHint'];
}
elseif (strpos($attributes['rel'], '#user.subscriptions')) {
$subscriptions = $attributes['countHint'];
}
elseif (strpos($attributes['rel'], '#user.liveevent')) {
$liveevents = $attributes['countHint'];
}
elseif (strpos($attributes['rel'], '#user.contacts')) {
$friends = $attributes['countHint'];
}
}
这将返回我正在寻找的正确值,但我现在担心我正在做额外的循环处理,因为我会假设每个循环都会测试每一行,无论它是否已经找到该值在上一个循环中?
【问题讨论】:
标签: php xml xpath youtube gdata