Parsing Alexa API with PHP -


i need extracting attributes amazon alexa api.

specifically, need extract attribute values "vn" this:

<aws:country code="vn"> 

i able fetch content, , simplexmlelement seems parse okay, brain cannot figure out how reference attributes.

here's sample of 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 full alexa xml response:

this xml file not appear have style information associated it. document tree 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> 

$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 $country) { foreach($country->attributes() $code => $value) { if ($code == 'code') { echo $value; } } echo ' - '; echo $country->rank, '<br />'; } }

and like
vn - 78
in - 18 , on


Comments

Popular posts from this blog

PySide and Qt Properties: Connecting signals from Python to QML -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

scala - 'wrong top statement declaration' when using slick in IntelliJ -