我的问题是使用PHP访问YouTube XML Feed中的数据。截断的Feed是这样的:
<?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:statistics:
$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 :(得分:-1)
你没有$entry
,改为:
$feed = simplexml_load_string($xml);
$ns = $feed->getNamespaces(true);
$yt = $feed->children($ns['yt']);
.....