Я пытаюсь получить XML-данные из Alexa Ranking url
http://data.alexa.com/data?cli=10&dat=s&url=google.com
этот единственный URL-адрес работает нормально, но когда я получаю несколько URL-адресов в массиве и перебираю их через foreach, он показывает только данные последнего URL-адреса вмассив.Код, который я использую:
$list = file_get_contents("sites.txt");
$urls = explode ("\n", $list);
foreach ($urls as $url) {
echo $url;echo "<br />";
$uri = 'http://data.alexa.com/data?cli=10&dat=s&url=';
$uri .= $url;
$xml = simplexml_load_file($uri,"SimpleXMLElement",LIBXML_NOCDATA);
print_r($xml);
if (isset($xml->SD[1])){
$data = (int) $xml->SD[1]->POPULARITY->attributes()->TEXT;
print_r($data);
}
else {echo "Not Found";echo "<br />";}
}
sites.txt содержит
google.com
facebook.com
archive.com
adjustedreality.com
adkforum.com
результат равен
google.com
SimpleXMLElement Object ( [@attributes] => Array ( [VER] => 0.9 [URL] => 404 [HOME] => 0 [AID] => = [IDN] => ) [0] => ) Not Found
facebook.com
SimpleXMLElement Object ( [@attributes] => Array ( [VER] => 0.9 [URL] => 404 [HOME] => 0 [AID] => = [IDN] => ) [0] => ) Not Found
archive.com
SimpleXMLElement Object ( [@attributes] => Array ( [VER] => 0.9 [URL] => 404 [HOME] => 0 [AID] => = [IDN] => ) [0] => ) Not Found
adjustedreality.com
SimpleXMLElement Object ( [@attributes] => Array ( [VER] => 0.9 [URL] => 404 [HOME] => 0 [AID] => = [IDN] => ) [0] => ) Not Found
adkforum.com
SimpleXMLElement Object ( [@attributes] => Array ( [VER] => 0.9 [URL] => adkforum.com/ [HOME] => 0 [AID] => = [IDN] => adkforum.com/ ) [SD] => Array ( [0] => SimpleXMLElement Object ( [@attributes] => Array ( [TITLE] => A [FLAGS] => [HOST] => adkforum.com ) [0] => ) [1] => SimpleXMLElement Object ( [POPULARITY] => SimpleXMLElement Object ( [@attributes] => Array ( [URL] => adkforum.com/ [TEXT] => 2054938 [SOURCE] => panel ) ) [REACH] =>
SimpleXMLElement Object ( [@attributes] => Array ( [RANK] => 2100659 ) ) [RANK] => SimpleXMLElement Object ( [@attributes] => Array ( [DELTA] => +800368 ) ) ) ) ) 2054938
Не имеет значения, если сайты.txt содержит 2 или 200 URL, он отображает только данные последнего URL в списке / массиве.