用 PHP 解析 Alexa API

Parsing Alexa API with PHP

我需要帮助从 Amazon Alexa 中提取属性 API。

具体来说,我需要从中提取 "VN" 这样的属性值:

<aws:Country Code="VN">

我能够获取内容,SimpleXMLElement 似乎可以解析它,但我的大脑就是不知道如何引用属性。

这是我的代码示例:

$xml = new SimpleXMLElement($response, null, false, 'http://awis.amazonaws.com/doc/2005-07-11');
if(count($xml)) 
{
    $cc = $xml->Response->UrlInfoResult->Alexa->TrafficData->RankByCountry->Country['VN'];
}

这是完整的 Alexa XML 回复:

此 XML 文件似乎没有任何关联的样式信息。文档树如下所示。

<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>1551fc65-c954-3af9-4cc1-a6c05a87bb98</aws:RequestId>
</aws:OperationRequest>
<aws:UrlInfoResult>
<aws:Alexa>
<aws:ContentData>
<aws:DataUrl type="canonical">whosebug.com</aws:DataUrl>
<aws:LinksInCount>66351</aws:LinksInCount>
</aws:ContentData>
<aws:TrafficData>
<aws:DataUrl type="canonical">whosebug.com</aws:DataUrl>
<aws:Rank>49</aws:Rank>
<aws:Rank>49</aws:Rank>
<aws:RankByCountry>
<aws:Country Code="VN">
<aws:Rank>78</aws:Rank>
<aws:Contribution>
<aws:PageViews>0.7%</aws:PageViews>
<aws:Users>0.6%</aws:Users>
</aws:Contribution>
</aws:Country>
<aws:Country Code="IN">
<aws:Rank>18</aws:Rank>
<aws:Contribution>
<aws:PageViews>20.1%</aws:PageViews>
<aws:Users>18.3%</aws:Users>
</aws:Contribution>
</aws:Country>
<aws:Country Code="ID">
<aws:Rank>68</aws:Rank>
<aws:Contribution>
<aws:PageViews>1.7%</aws:PageViews>
<aws:Users>1.7%</aws:Users>
</aws:Contribution>
</aws:Country>
<aws:Country Code="EG">
<aws:Rank>84</aws:Rank>
<aws:Contribution>
<aws:PageViews>1.0%</aws:PageViews>
<aws:Users>0.9%</aws:Users>
</aws:Contribution>
</aws:Country>
<aws:Country Code="TH">
<aws:Rank>76</aws:Rank>
<aws:Contribution>
<aws:PageViews>0.6%</aws:PageViews>
<aws:Users>0.6%</aws:Users>
</aws:Contribution>
</aws:Country>
<aws:Country Code="BD">
<aws:Rank>35</aws:Rank>
<aws:Contribution>
<aws:PageViews>0.6%</aws:PageViews>
<aws:Users>0.7%</aws:Users>
</aws:Contribution>
</aws:Country>
<aws:Country Code="TR">
<aws:Rank>44</aws:Rank>
<aws:Contribution>
<aws:PageViews>3.0%</aws:PageViews>
<aws:Users>2.4%</aws:Users>
</aws:Contribution>
</aws:Country>
<aws:Country Code="MY">
<aws:Rank>58</aws:Rank>
<aws:Contribution>
<aws:PageViews>0.5%</aws:PageViews>
<aws:Users>0.6%</aws:Users>
</aws:Contribution>
</aws:Country>
<aws:Country Code="RU">
<aws:Rank>35</aws:Rank>
<aws:Contribution>
<aws:PageViews>5.2%</aws:PageViews>
<aws:Users>5.0%</aws:Users>
</aws:Contribution>
</aws:Country>
<aws:Country Code="IL">
<aws:Rank>25</aws:Rank>
<aws:Contribution>
<aws:PageViews>0.7%</aws:PageViews>
<aws:Users>0.6%</aws:Users>
</aws:Contribution>
</aws:Country>
<aws:Country Code="TW">
<aws:Rank>128</aws:Rank>
<aws:Contribution>
<aws:PageViews>0.9%</aws:PageViews>
<aws:Users>0.9%</aws:Users>
</aws:Contribution>
</aws:Country>
<aws:Country Code="ES">
<aws:Rank>71</aws:Rank>
<aws:Contribution>
<aws:PageViews>1.8%</aws:PageViews>
<aws:Users>1.8%</aws:Users>
</aws:Contribution>
</aws:Country>
<aws:Country Code="PK">
<aws:Rank>26</aws:Rank>
<aws:Contribution>
<aws:PageViews>1.9%</aws:PageViews>
<aws:Users>1.9%</aws:Users>
</aws:Contribution>
</aws:Country>
<aws:Country Code="US">
<aws:Rank>68</aws:Rank>
<aws:Contribution>
<aws:PageViews>13.1%</aws:PageViews>
<aws:Users>15.4%</aws:Users>
</aws:Contribution>
</aws:Country>
<aws:Country Code="HK">
<aws:Rank>53</aws:Rank>
<aws:Contribution>
<aws:PageViews>0.8%</aws:PageViews>
<aws:Users>0.7%</aws:Users>
</aws:Contribution>
</aws:Country>
<aws:Country Code="RO">
<aws:Rank>40</aws:Rank>
<aws:Contribution>
<aws:PageViews>0.6%</aws:PageViews>
<aws:Users>0.5%</aws:Users>
</aws:Contribution>
</aws:Country>
<aws:Country Code="IR">
<aws:Rank>34</aws:Rank>
<aws:Contribution>
<aws:PageViews>3.5%</aws:PageViews>
<aws:Users>3.0%</aws:Users>
</aws:Contribution>
</aws:Country>
<aws:Country Code="AU">
<aws:Rank>64</aws:Rank>
<aws:Contribution>
<aws:PageViews>1.0%</aws:PageViews>
<aws:Users>1.0%</aws:Users>
</aws:Contribution>
</aws:Country>
<aws:Country Code="SA">
<aws:Rank>104</aws:Rank>
<aws:Contribution>
<aws:PageViews>0.6%</aws:PageViews>
<aws:Users>0.6%</aws:Users>
</aws:Contribution>
</aws:Country>
<aws:Country Code="PL">
<aws:Rank>47</aws:Rank>
<aws:Contribution>
<aws:PageViews>1.6%</aws:PageViews>
<aws:Users>1.4%</aws:Users>
</aws:Contribution>
</aws:Country>
<aws:Country Code="NG">
<aws:Rank>44</aws:Rank>
<aws:Contribution>
<aws:PageViews>0.9%</aws:PageViews>
<aws:Users>1.0%</aws:Users>
</aws:Contribution>
</aws:Country>
<aws:Country Code="GB">
<aws:Rank>67</aws:Rank>
<aws:Contribution>
<aws:PageViews>2.3%</aws:PageViews>
<aws:Users>2.5%</aws:Users>
</aws:Contribution>
</aws:Country>
<aws:Country Code="FR">
<aws:Rank>105</aws:Rank>
<aws:Contribution>
<aws:PageViews>1.6%</aws:PageViews>
<aws:Users>1.6%</aws:Users>
</aws:Contribution>
</aws:Country>
<aws:Country Code="KR">
<aws:Rank>38</aws:Rank>
<aws:Contribution>
<aws:PageViews>3.4%</aws:PageViews>
<aws:Users>3.8%</aws:Users>
</aws:Contribution>
</aws:Country>
<aws:Country Code="UA">
<aws:Rank>29</aws:Rank>
<aws:Contribution>
<aws:PageViews>1.7%</aws:PageViews>
<aws:Users>1.6%</aws:Users>
</aws:Contribution>
</aws:Country>
<aws:Country Code="BR">
<aws:Rank>63</aws:Rank>
<aws:Contribution>
<aws:PageViews>2.5%</aws:PageViews>
<aws:Users>2.4%</aws:Users>
</aws:Contribution>
</aws:Country>
<aws:Country Code="IT">
<aws:Rank>65</aws:Rank>
<aws:Contribution>
<aws:PageViews>1.9%</aws:PageViews>
<aws:Users>1.9%</aws:Users>
</aws:Contribution>
</aws:Country>
<aws:Country Code="GR">
<aws:Rank>107</aws:Rank>
<aws:Contribution>
<aws:PageViews>0.5%</aws:PageViews>
<aws:Users>0.6%</aws:Users>
</aws:Contribution>
</aws:Country>
<aws:Country Code="ZA">
<aws:Rank>47</aws:Rank>
<aws:Contribution>
<aws:PageViews>0.7%</aws:PageViews>
<aws:Users>0.8%</aws:Users>
</aws:Contribution>
</aws:Country>
<aws:Country Code="BY">
<aws:Rank>22</aws:Rank>
<aws:Contribution>
<aws:PageViews>0.8%</aws:PageViews>
<aws:Users>0.6%</aws:Users>
</aws:Contribution>
</aws:Country>
<aws:Country Code="MX">
<aws:Rank>138</aws:Rank>
<aws:Contribution>
<aws:PageViews>0.7%</aws:PageViews>
<aws:Users>0.7%</aws:Users>
</aws:Contribution>
</aws:Country>
<aws:Country Code="NL">
<aws:Rank>59</aws:Rank>
<aws:Contribution>
<aws:PageViews>0.8%</aws:PageViews>
<aws:Users>0.7%</aws:Users>
</aws:Contribution>
</aws:Country>
<aws:Country Code="CA">
<aws:Rank>64</aws:Rank>
<aws:Contribution>
<aws:PageViews>1.1%</aws:PageViews>
<aws:Users>1.3%</aws:Users>
</aws:Contribution>
</aws:Country>
<aws:Country Code="JP">
<aws:Rank>407</aws:Rank>
<aws:Contribution>
<aws:PageViews>1.0%</aws:PageViews>
<aws:Users>1.4%</aws:Users>
</aws:Contribution>
</aws:Country>
<aws:Country Code="CN">
<aws:Rank>116</aws:Rank>
<aws:Contribution>
<aws:PageViews>4.7%</aws:PageViews>
<aws:Users>5.9%</aws:Users>
</aws:Contribution>
</aws:Country>
<aws:Country Code="O">
<aws:Rank/>
<aws:Contribution>
<aws:PageViews>13.5%</aws:PageViews>
<aws:Users>13.0%</aws:Users>
</aws:Contribution>
</aws:Country>
<aws:Country Code="DE">
<aws:Rank>112</aws:Rank>
<aws:Contribution>
<aws:PageViews>1.7%</aws:PageViews>
<aws:Users>1.8%</aws:Users>
</aws:Contribution>
</aws:Country>
</aws:RankByCountry>
<aws:UsageStatistics>
<aws:UsageStatistic>
<aws:TimeRange>
<aws:Months>3</aws:Months>
</aws:TimeRange>
<aws:Rank>
<aws:Value>49</aws:Value>
<aws:Delta>-8</aws:Delta>
</aws:Rank>
<aws:Reach>
<aws:Rank>
<aws:Value>49</aws:Value>
<aws:Delta>-2</aws:Delta>
</aws:Rank>
<aws:PerMillion>
<aws:Value>16,260</aws:Value>
<aws:Delta>+6.8%</aws:Delta>
</aws:PerMillion>
</aws:Reach>
<aws:PageViews>
<aws:PerMillion>
<aws:Value>1,021.6</aws:Value>
<aws:Delta>+4.01%</aws:Delta>
</aws:PerMillion>
<aws:Rank>
<aws:Value>73</aws:Value>
<aws:Delta>-3</aws:Delta>
</aws:Rank>
<aws:PerUser>
<aws:Value>4.36</aws:Value>
<aws:Delta>-2.68%</aws:Delta>
</aws:PerUser>
</aws:PageViews>
</aws:UsageStatistic>
<aws:UsageStatistic>
<aws:TimeRange>
<aws:Months>1</aws:Months>
</aws:TimeRange>
<aws:Rank>
<aws:Value>51</aws:Value>
<aws:Delta>+2</aws:Delta>
</aws:Rank>
<aws:Reach>
<aws:Rank>
<aws:Value>50</aws:Value>
<aws:Delta>+1</aws:Delta>
</aws:Rank>
<aws:PerMillion>
<aws:Value>15,780</aws:Value>
<aws:Delta>-7.59%</aws:Delta>
</aws:PerMillion>
</aws:Reach>
<aws:PageViews>
<aws:PerMillion>
<aws:Value>1,031.2</aws:Value>
<aws:Delta>-7.33%</aws:Delta>
</aws:PerMillion>
<aws:Rank>
<aws:Value>75</aws:Value>
<aws:Delta>1</aws:Delta>
</aws:Rank>
<aws:PerUser>
<aws:Value>4.36</aws:Value>
<aws:Delta>+0.2%</aws:Delta>
</aws:PerUser>
</aws:PageViews>
</aws:UsageStatistic>
<aws:UsageStatistic>
<aws:TimeRange>
<aws:Days>7</aws:Days>
</aws:TimeRange>
<aws:Rank>
<aws:Value>50</aws:Value>
<aws:Delta>0</aws:Delta>
</aws:Rank>
<aws:Reach>
<aws:Rank>
<aws:Value>51</aws:Value>
<aws:Delta>+3</aws:Delta>
</aws:Rank>
<aws:PerMillion>
<aws:Value>15,770</aws:Value>
<aws:Delta>-2.89%</aws:Delta>
</aws:PerMillion>
</aws:Reach>
<aws:PageViews>
<aws:PerMillion>
<aws:Value>1,047</aws:Value>
<aws:Delta>-3.74%</aws:Delta>
</aws:PerMillion>
<aws:Rank>
<aws:Value>75</aws:Value>
<aws:Delta>5</aws:Delta>
</aws:Rank>
<aws:PerUser>
<aws:Value>4.39</aws:Value>
<aws:Delta>-0.90%</aws:Delta>
</aws:PerUser>
</aws:PageViews>
</aws:UsageStatistic>
<aws:UsageStatistic>
<aws:TimeRange>
<aws:Days>1</aws:Days>
</aws:TimeRange>
<aws:Rank>
<aws:Value>96</aws:Value>
<aws:Delta>+1</aws:Delta>
</aws:Rank>
<aws:Reach>
<aws:Rank>
<aws:Value>89</aws:Value>
<aws:Delta>0</aws:Delta>
</aws:Rank>
<aws:PerMillion>
<aws:Value>9,180</aws:Value>
<aws:Delta>+0.6%</aws:Delta>
</aws:PerMillion>
</aws:Reach>
<aws:PageViews>
<aws:PerMillion>
<aws:Value>629</aws:Value>
<aws:Delta>+4.2%</aws:Delta>
</aws:PerMillion>
<aws:Rank>
<aws:Value>117</aws:Value>
<aws:Delta>1</aws:Delta>
</aws:Rank>
<aws:PerUser>
<aws:Value>4.37</aws:Value>
<aws:Delta>+4%</aws:Delta>
</aws:PerUser>
</aws:PageViews>
</aws:UsageStatistic>
</aws:UsageStatistics>
<aws:ContributingSubdomains>
<aws:ContributingSubdomain>
<aws:DataUrl>whosebug.com</aws:DataUrl>
<aws:TimeRange>
<aws:Months>1</aws:Months>
</aws:TimeRange>
<aws:Reach>
<aws:Percentage>99.00%</aws:Percentage>
</aws:Reach>
<aws:PageViews>
<aws:Percentage>98.85%</aws:Percentage>
<aws:PerUser>4.35</aws:PerUser>
</aws:PageViews>
</aws:ContributingSubdomain>
<aws:ContributingSubdomain>
<aws:DataUrl>ru.whosebug.com</aws:DataUrl>
<aws:TimeRange>
<aws:Months>1</aws:Months>
</aws:TimeRange>
<aws:Reach>
<aws:Percentage>1.37%</aws:Percentage>
</aws:Reach>
<aws:PageViews>
<aws:Percentage>0.65%</aws:Percentage>
<aws:PerUser>2.06</aws:PerUser>
</aws:PageViews>
</aws:ContributingSubdomain>
<aws:ContributingSubdomain>
<aws:DataUrl>pt.whosebug.com</aws:DataUrl>
<aws:TimeRange>
<aws:Months>1</aws:Months>
</aws:TimeRange>
<aws:Reach>
<aws:Percentage>0.37%</aws:Percentage>
</aws:Reach>
<aws:PageViews>
<aws:Percentage>0.17%</aws:Percentage>
<aws:PerUser>2.06</aws:PerUser>
</aws:PageViews>
</aws:ContributingSubdomain>
<aws:ContributingSubdomain>
<aws:DataUrl>es.whosebug.com</aws:DataUrl>
<aws:TimeRange>
<aws:Months>1</aws:Months>
</aws:TimeRange>
<aws:Reach>
<aws:Percentage>0.05%</aws:Percentage>
</aws:Reach>
<aws:PageViews>
<aws:Percentage>0.07%</aws:Percentage>
<aws:PerUser>6.0</aws:PerUser>
</aws:PageViews>
</aws:ContributingSubdomain>
<aws:ContributingSubdomain>
<aws:DataUrl>meta.whosebug.com</aws:DataUrl>
<aws:TimeRange>
<aws:Months>1</aws:Months>
</aws:TimeRange>
<aws:Reach>
<aws:Percentage>0.15%</aws:Percentage>
</aws:Reach>
<aws:PageViews>
<aws:Percentage>0.06%</aws:Percentage>
<aws:PerUser>1.80</aws:PerUser>
</aws:PageViews>
</aws:ContributingSubdomain>
<aws:ContributingSubdomain>
<aws:DataUrl>chat.whosebug.com</aws:DataUrl>
<aws:TimeRange>
<aws:Months>1</aws:Months>
</aws:TimeRange>
<aws:Reach>
<aws:Percentage>0.10%</aws:Percentage>
</aws:Reach>
<aws:PageViews>
<aws:Percentage>0.06%</aws:Percentage>
<aws:PerUser>2.4</aws:PerUser>
</aws:PageViews>
</aws:ContributingSubdomain>
<aws:ContributingSubdomain>
<aws:DataUrl>OTHER</aws:DataUrl>
<aws:TimeRange>
<aws:Months>1</aws:Months>
</aws:TimeRange>
<aws:Reach>
<aws:Percentage>0</aws:Percentage>
</aws:Reach>
<aws:PageViews>
<aws:Percentage>0.15%</aws:Percentage>
<aws:PerUser>0</aws:PerUser>
</aws:PageViews>
</aws:ContributingSubdomain>
</aws:ContributingSubdomains>
</aws:TrafficData>
</aws:Alexa>
</aws:UrlInfoResult>
<aws:ResponseStatus xmlns:aws="http://alexa.amazonaws.com/doc/2005-10-05/">
<aws:StatusCode>Success</aws:StatusCode>
</aws:ResponseStatus>
</aws:Response>
</aws:UrlInfoResponse>

您需要使用 SimpleXMLElement::attributes ,即:

foreach($xml->Country[0]->attributes() as $a => $b) {
    echo "$a=$b";
}
//Code=VN

$xml = new SimpleXMLElement($response,null,false, '<a href="http://awis.amazonaws.com/doc/2005-07-11" rel="noreferrer">http://awis.amazonaws.com/doc/2005-07-11</a>');</p>

    if($xml->count() && $xml->Response->UrlInfoResult->Alexa->count()) {
        $info = $xml->Response->UrlInfoResult->Alexa;
        foreach ($info->TrafficData->RankByCountry->Country as $country)
        {
            foreach($country->attributes() as $code => $value)
            {
                if ($code == 'Code') {
                    echo $value;
                }
            }
            echo ' - ';
            echo $country->Rank, '<br />';
        }
    }

你会得到类似
的东西 越南 - 78
IN - 18 等等

使用 php DOMDocument

的快捷方式
$xml = file_get_contents('http://awis.amazonaws.com/doc/2005-07-11');
$doc = new DOMDocument();
$doc->loadXML($xml);
$countries = $doc->getElementsByTagName('Country');
foreach ($countries as $country) {
    echo $country->getAttribute('Code');
}

您可以根据此test Fiddle

中提供的XML字符串检查模拟输出