2014-01-22 85 views
0

我試圖優化一個我爲解析YouTube配置文件而編寫的小型php應用程序。輸入一個YouTube用戶名,應用程序通過解析由帳戶配置文件的gdata查詢返回的XML返回一個簡單的上傳視頻數量,收藏夾,訂閱者數量列表。 例如這個XML:PHP和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

您正在使用foreach解析XML數據的正確路徑。我會在每個feedlink上做一個strpos(),直到找到uploads元素。然後我會設置$uploadscount

事情是這樣的,也許:

foreach ($gd->feedLink as $feedLink) { 
    $attributes = $feedlink->attributes(); 
    if (strpos($attributes['rel'], '#user.uploads')) { 
    $uploadscount = $attributes; 
    break; 
    } 
    continue; 
} 
+0

我認爲這是幫助和我取得了一些進展。由於'$ uploadscount = $ attributes;'顯示的是網址而不是值,並且中斷繼續不能與多個值一起使用,所以我不得不修改它。 – epontius

相關問題