【问题标题】:PHP With Youtube XML APIPHP 与 Youtube XML API
【发布时间】:2014-08-02 09:06:42
【问题描述】:

我的问题是关于使用 PHP 从 YouTube XML 提要访问数据。截断的提要是这样的:

<?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

有人可以指出我出了什么问题吗?谢谢!

【问题讨论】:

  • 也许你想要$yt = $feed-&gt;children($ns['yt']);$entry 未在脚本中的任何位置定义。
  • 不要使用文档中的前缀(别名)从 XML 中读取名称空间。前缀不是标识符 - 命名空间是。只需将命名空间 http://gdata.youtube.com/schemas/2007 写入源代码中的常量/变量并使用它。
  • 谁如此卑鄙地投反对票?解释一下。

标签: php xml youtube


【解决方案1】:

你没有$entry,改成:

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

【讨论】:

    猜你喜欢
    • 2013-07-30
    • 2012-05-12
    • 1970-01-01
    • 2011-09-27
    • 1970-01-01
    • 2018-03-01
    • 2012-04-05
    • 1970-01-01
    相关资源
    最近更新 更多