php - simplexml_load_file 不会使用数组获取数据
问题描述
我正在尝试从 Alexa Ranking url 获取 xml 数据
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
如果 sites.txt 包含 2 个或 200 个 url,则无关紧要,它只会显示列表/数组中最后一个 url 的数据。
解决方案
由于您的文件可能包含其他奇数字符(包括 \r、空格等),因此最好确保使用trim()
...
$uri .= trim($url);
推荐阅读
- vue.js - 在子元素中禁用 Vuetify v-touch
- javascript - 为什么我的正则表达式只在某些浏览器中有效?
- ruby-on-rails - 使用 Ruby on Rails 向博客文章添加多个图像或不添加图像
- excel - 如果达到一个单元格的限制,则 Vba 写入下一个单元格
- android - Android 版本构建在安装时崩溃
- apache-kafka-streams - Kafka Streams 状态存储无法从更改日志主题中恢复
- shopify - Shopify 液体 - 通过字符串获取产品
- javascript - 如何创建类似于 string.split() 的自定义函数来反转字符串
- html - 在 css 中使用 :has 和 ::before
- lilypond - Lilypond "<< ... >>" 结构有什么作用?