解析 Google Analytics API XML 响应
我似乎无法解析有机关键字的指标。我使用 simplexml_load_string 加载整个响应字符串,然后当我 print_r 对象时,我可以看到以下内容:
[0] => SimpleXMLElement Object
(
[id] => http://www.google.com/analytics/feeds/data?ids=ga:480&ga:keyword=cats&start-date=2011-04-10&end-date=2011-07-24
[updated] => 2011-07-23T17:00:00.001-07:00
[title] => ga:keyword=cats
[link] => SimpleXMLElement Object
(
[@attributes] => Array
(
[rel] => alternate
[type] => text/html
[href] => http://www.google.com/analytics
)
)
)
但是当我查看实际(预格式化)响应时,我看到一些无法在格式中保存的额外数据:
<entry gd:etag="W/"hdSFEs."" gd:kind="analytics#datarow">
<id>http://www.google.com/analytics/feeds/data?ids=ga:430&ga:keyword=cats&start-date=2011-04-10&end-date=2011-07-24</id>
<updated>2011-07-23T17:00:00.001-07:00</updated>
<title>ga:keyword=cats</title>
<link rel="alternate" type="text/html" href="http://www.google.com/analytics"/>
<dxp:dimension name="ga:keyword" value="cats"/>
<dxp:metric confidenceInterval="0.0" name="ga:organicSearches" type="integer" value="2"/>
</entry>
我需要底部的度量值,在本例中为 2。我怎样才能得到它?
谢谢!
I cannot seem to parse out the metrics for an organic keyword. I am using simplexml_load_string to load the whole response string and then when I print_r the object I can see this:
[0] => SimpleXMLElement Object
(
[id] => http://www.google.com/analytics/feeds/data?ids=ga:480&ga:keyword=cats&start-date=2011-04-10&end-date=2011-07-24
[updated] => 2011-07-23T17:00:00.001-07:00
[title] => ga:keyword=cats
[link] => SimpleXMLElement Object
(
[@attributes] => Array
(
[rel] => alternate
[type] => text/html
[href] => http://www.google.com/analytics
)
)
)
But when I look at the actual (pre-formatted) response I see some extra data that didn't survive the format:
<entry gd:etag="W/"hdSFEs."" gd:kind="analytics#datarow">
<id>http://www.google.com/analytics/feeds/data?ids=ga:430&ga:keyword=cats&start-date=2011-04-10&end-date=2011-07-24</id>
<updated>2011-07-23T17:00:00.001-07:00</updated>
<title>ga:keyword=cats</title>
<link rel="alternate" type="text/html" href="http://www.google.com/analytics"/>
<dxp:dimension name="ga:keyword" value="cats"/>
<dxp:metric confidenceInterval="0.0" name="ga:organicSearches" type="integer" value="2"/>
</entry>
I need that metric value at the bottom, in this case it's two. How can I get it?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
编辑: Arend 是对的,问题只是 SimpleXML 奇怪地处理名称空间。
您可能需要将整个 XML 响应传递给 SimpleXMLElement() 构造函数,而不仅仅是代码片段你在这里提供的。特别是,您需要
元素,因为它具有命名空间声明:如果没有这些命名空间声明,您的 XML 库就会删除任何命名空间属性或元素(例如
)。您还可能从不同版本的 XML 库获得不同的结果。在我的系统上,您的代码片段会生成一个包含所有属性和元素的 SimpleXMLElement,包括
,尽管我确实收到了一堆 PHP 警告,抱怨缺少名称空间声明。Edit: Arend is right, the problem is just SimpleXML handling the namespaces strangely.
You might need to pass the entire XML response to the SimpleXMLElement() constructor, not just the snippet you provided here. In particular you need the
<feed>
element because it has the namespace declarations:Without these namespace declarations it looks like your XML library is dropping any namespaced attributes or elements (such as
<dxp:metric>
).You might also get different results from a different version of the XML library. On my system your snippet produces a SimpleXMLElement with all of the attributes and elements, including
<metric>
, though I do get a bunch of PHP warnings complaining about the missing namespace declarations.编辑:简单的解决方法是这样的:
var_dump($object->xpath('dxp:metric'));
问题是 simplexml 有一个“陷阱”与命名空间。正如 kudo 对 squirrel 的说法,您确实需要使用命名空间,但我有预感,您只发布了 xml 响应的一部分,而不是整个响应。
这里有一篇关于它的优秀文章: http://blogs.sitepoint.com/simplexml- and-namespaces/
为了获取指标的值(以及指标本身),您可以开始使用这段代码。
Be aware that there are some attributes in there that are again in a different namespace.
其余的也已在这里得到解答:使用 SimpleXML 使用命名空间解析 XML
Edit: the easy way out is this:
var_dump($object->xpath('dxp:metric'));
The problem is that simplexml has a 'gotcha' with namespaces. As kudo's to squirrel, you indeed need to use namespaces, but I have a hunch that you only posted a part of the xml response and not the whole response.
There is an excellent post about it here: http://blogs.sitepoint.com/simplexml-and-namespaces/
In order to fetch the value of the metric (and the metric itself), you can get started with this piece of code.
Be aware that there are some attributes in there that are again in a different namespace.
The rest also has been answered here: Parse XML with Namespace using SimpleXML