2014-06-12 97 views
0

我的問題是關於使用PHP從YouTube XML提要訪問數據。截短的飼料是這樣的:PHP使用Youtube XML API

<?xml version="1.0" encoding="UTF-8"?> 
<entry xmlns="http://www.w3.org/2005/Atom" xmlns:gd="http://schemas.google.com/g/2005" xmlns:media="http://search.yahoo.com/mrss/" xmlns:yt="http://gdata.youtube.com/schemas/2007"> 
    <id>http://gdata.youtube.com/feeds/api/users/AaiKcIfHEzUZl34U980sNA</id> 
    <yt:firstName>wahbanana</yt:firstName> 
    <yt:googlePlusUserId>118419787126790739507</yt:googlePlusUserId> 
    <yt:location>SG</yt:location> 
    <yt:statistics lastWebAccess="1970-01-01T00:00:00.000Z" subscriberCount="353936" videoWatchCount="0" viewCount="0" totalUploadViews="40288085" /> 
    <media:thumbnail url="http://yt3.ggpht.com/-ukikCGaaWTw/AAAAAAAAAAI/AAAAAAAAAAA/zmPZoDtJEK0/s88-c-k-no/photo.jpg" /> 
    <yt:username>wahbanana</yt:username> 
</entry> 

我寫了一個函數來獲取subscribeCount iinside YT:統計:

$url = 'http://gdata.youtube.com/feeds/api/users/'.$yt_username; 
$xml = file_get_contents($url); 
$feed = simplexml_load_string($xml); 
$ns=$feed->getNameSpaces(true); 

$yt = $entry->children($ns['yt']); 
$yt_statistics = $yt->statistics->attributes(); 
$yt_subscribers = $yt_statistics['subscriberCount']; 

但它返回此錯誤:

PHP Fatal error: Call to a member function children() on a non-object in /home/digitali/public_html/wp-content/plugins/OCG-socialintegration/ocg_si.php on line 111 

有人能指出對我來說有什麼不對?謝謝!

+0

也許你想'$ YT = $饋>兒童($ NS [ 'YT']);'。 '$ entry'在腳本的任何地方都沒有定義。 –

+0

什麼是$條目..? –

+0

不要使用文檔中的前綴(別名)從XML中讀取名稱空間。前綴不是標識符 - 名稱空間是。只需在源代碼中的常量/變量中寫入命名空間'http:// gdata.youtube.com/schemas/2007'並使用它。 – ThW

回答

-1

你沒有$entry,更改爲:

$feed = simplexml_load_string($xml); 
$ns = $feed->getNamespaces(true); 
$yt = $feed->children($ns['yt']); 
..... 
+0

是的,我剛從kevinabelita的回答中意識到。謝謝 :)) – Calvintwr