Wonko the Sane Wonko the Sane - 6 months ago 13
PHP Question

Parsing Alexa API with PHP

I need help extracting attributes from the Amazon Alexa API.

Specifically, I need to extract attribute values like "VN" from this:

<aws:Country Code="VN">


I am able to fetch the content, and SimpleXMLElement seems to parse it okay, but my brain just cannot figure out how to reference the attributes.

Here's a sample of my code:

$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'];
}


Here is the full Alexa XML response:

This XML file does not appear to have any style information associated with it. The document tree is shown below.

<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">stackoverflow.com</aws:DataUrl>
<aws:LinksInCount>66351</aws:LinksInCount>
</aws:ContentData>
<aws:TrafficData>
<aws:DataUrl type="canonical">stackoverflow.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>stackoverflow.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.stackoverflow.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.stackoverflow.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.stackoverflow.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.stackoverflow.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.stackoverflow.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>

Answer

$xml = new SimpleXMLElement($response,null,false, 'http://awis.amazonaws.com/doc/2005-07-11');

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 />'; } }

and you get something like
VN - 78
IN - 18 and so on

Comments