我正在尝试优化我为解析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'];
}
}
这将返回我正在寻找的正确值,但我现在担心我正在做循环中的额外处理,因为我会假设每个循环测试每一行,无论它是否已经找到该值在之前的循环?
答案 0 :(得分:0)
使用foreach解析XML数据是正确的。我会在每个strpos()
上执行feedlink
,直到找到uploads元素。然后我会设置$uploadscount
。
像这样的东西,也许:
foreach ($gd->feedLink as $feedLink) {
$attributes = $feedlink->attributes();
if (strpos($attributes['rel'], '#user.uploads')) {
$uploadscount = $attributes;
break;
}
continue;
}