Alexa api 站点信息 xml 格式转换为 php 值

发布于 2025-01-02 23:01:17 字数 1624 浏览 0 评论 0原文

我尝试使用亚马逊的 Alexa api。我想接收网站数据。

我创建一个 url 来接收数据,我以 xml 格式获取该数据。

当我尝试读取此文件时,

$xml = simplexml_load_file('test.xml');
print_r($xml);

我只得到代码“SimpleXMLElement Object ()”。

如何从这个 xml 获取数据到 php?

我得到的 xml 文件的一部分;

    <?xml version="1.0"?>
<aws:UrlInfoResponse xmlns:aws="http://alexa.amazonaws.com/doc/2005-10-05/"><aws:Response xmlns:aws="http://awis.amazonaws.com/doc/2005-07-11"><aws:OperationRequest><aws:RequestId>removed</aws:RequestId></aws:OperationRequest><aws:UrlInfoResult><aws:Alexa>

  <aws:TrafficData>
    <aws:DataUrl type="canonical">yahoo.com</aws:DataUrl>
    <aws:Rank>4</aws:Rank>
    <aws:UsageStatistics>
      <aws:UsageStatistic>
        <aws:TimeRange>

      <aws:Months>3</aws:Months>
    </aws:TimeRange>
    <aws:Rank>
      <aws:Value>4</aws:Value>
      <aws:Delta>0</aws:Delta>
    </aws:Rank>
    <aws:Reach>

      <aws:Rank>
        <aws:Value>4</aws:Value>
        <aws:Delta>0</aws:Delta>
      </aws:Rank>
      <aws:PerMillion>
        <aws:Value>228,260</aws:Value>
        <aws:Delta>-2.83%</aws:Delta>

      </aws:PerMillion>
    </aws:Reach>
    <aws:PageViews>
      <aws:PerMillion>
        <aws:Value>14,390</aws:Value>
        <aws:Delta>-10.59%</aws:Delta>
      </aws:PerMillion>
      <aws:Rank>

I try to make use of the alexa api by amazon. I want to receive website data.

I create a url to receive the data, I get this in a xml format.

When I try to read this file by

$xml = simplexml_load_file('test.xml');
print_r($xml);

I get just the code 'SimpleXMLElement Object ( ) '.

How can I get the data to php from this xml?

A Part of the xml file I get ;

    <?xml version="1.0"?>
<aws:UrlInfoResponse xmlns:aws="http://alexa.amazonaws.com/doc/2005-10-05/"><aws:Response xmlns:aws="http://awis.amazonaws.com/doc/2005-07-11"><aws:OperationRequest><aws:RequestId>removed</aws:RequestId></aws:OperationRequest><aws:UrlInfoResult><aws:Alexa>

  <aws:TrafficData>
    <aws:DataUrl type="canonical">yahoo.com</aws:DataUrl>
    <aws:Rank>4</aws:Rank>
    <aws:UsageStatistics>
      <aws:UsageStatistic>
        <aws:TimeRange>

      <aws:Months>3</aws:Months>
    </aws:TimeRange>
    <aws:Rank>
      <aws:Value>4</aws:Value>
      <aws:Delta>0</aws:Delta>
    </aws:Rank>
    <aws:Reach>

      <aws:Rank>
        <aws:Value>4</aws:Value>
        <aws:Delta>0</aws:Delta>
      </aws:Rank>
      <aws:PerMillion>
        <aws:Value>228,260</aws:Value>
        <aws:Delta>-2.83%</aws:Delta>

      </aws:PerMillion>
    </aws:Reach>
    <aws:PageViews>
      <aws:PerMillion>
        <aws:Value>14,390</aws:Value>
        <aws:Delta>-10.59%</aws:Delta>
      </aws:PerMillion>
      <aws:Rank>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萝莉病 2025-01-09 23:01:17

这或多或少是您所需要的:

    $xml = new SimpleXMLElement($response, null, false, 'http://awis.amazonaws.com/doc/2005-10-05');
    if(count($xml)) 
    {
        $info = (string)$xml->TrafficData->DataUrl->Alexa;
    }

注意:

  • 我正在命名空间中提供: 'http://awis.amazonaws.com/doc/2005-10-05'
  • 将路径链接到您想要的元素,其中 $ xml 是根元素。
  • 将元素转换为字符串以获取其文本节点

this is more or less what you need:

    $xml = new SimpleXMLElement($response, null, false, 'http://awis.amazonaws.com/doc/2005-10-05');
    if(count($xml)) 
    {
        $info = (string)$xml->TrafficData->DataUrl->Alexa;
    }

notes:

  • i'm feeding in the namespace: 'http://awis.amazonaws.com/doc/2005-10-05'
  • chain a path to the element that you want, where $xml is the root element.
  • cast an element to string to get its text node
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文