phpxmlamazon-web-servicesalexa-internet

Alexa api site info xml format to php values


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>

Solution

  • 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: