使用 PHP 解析 XML 时,它仅显示文件中的第一条记录,而不是全部记录
下面是我的代码:
foreach(simplexml_load_file('http://www.bbc.co.uk/radio1/playlist.xml')->item as $link){
$linked = $link->artist;
$xml_data = file_get_contents('http://ws.audioscrobbler.com/2.0/?method=artist.getimages&artist=' . $linked . '&api_key=b25b959554ed76058ac220b7b2e0a026');
$xml = new SimpleXMLElement($xml_data);
foreach($xml->images as $test){
$new = $test->image->sizes->size[4];
echo "<img src='$new'>";
?><br /><?php
}}
?>
这确实有效,但它只显示许多记录中的一条记录,它显示 XML 文件中的第一条记录。我希望它显示所有记录。
我试图从这段代码中实现的是:
我有一个 xml 文件,我从中获取艺术家姓名,然后列出所有艺术家姓名并将它们插入到一个链接中,因此该链接是根据生成的艺术家姓名动态创建的。然后,我想要获取动态创建的链接(这是另一个 xml 文件)并解析该文件以获取大小节点,该节点是图像链接(图像是艺术家的图像)。然后,我想将该图像链接回显到显示该图像的图像标签中。
它部分有效,但正如我之前所说,它只显示一条记录,而不是 xml 文件中的所有记录。
Below is my code:
foreach(simplexml_load_file('http://www.bbc.co.uk/radio1/playlist.xml')->item as $link){
$linked = $link->artist;
$xml_data = file_get_contents('http://ws.audioscrobbler.com/2.0/?method=artist.getimages&artist=' . $linked . '&api_key=b25b959554ed76058ac220b7b2e0a026');
$xml = new SimpleXMLElement($xml_data);
foreach($xml->images as $test){
$new = $test->image->sizes->size[4];
echo "<img src='$new'>";
?><br /><?php
}}
?>
This does work, but it only displays one record from many, it shows the first record from the XML file. I want it to display all of the records.
What I am trying to achieve from this code is:
I have an xml file I am getting the artist name from, im then listing all of the artist names and inserting them into a link which is therefore dynamically created from them generated artist names. I then want to take the dynamically created link, which is another xml file and parse that file to get the size node which is an image link (the image is of the artist). I then want to echo that image link out into an image tag which displays the image.
It partially works, but as I said earlier, It only displays one record instead of all the records in the xml file.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
返回的 XML 结构如下:
这意味着您必须迭代
示例:
顺便说一句,没有理由在那里使用
file_get_contents
。使用simplexml_load_file
或使用new SimpleXmlElement('http://...', false, true)
。真的没有冒犯,但考虑到我已经在 当从 XML 文件中提取艺术家姓名时,仅显示 1 条记录 我强烈建议您尝试了解那里发生的情况,而不仅仅是复制和粘贴。The returned XML is structured like this:
Which means you have to iterate
Example:
On a sidenote, there is no reason to use
file_get_contents
there. Either usesimplexml_load_file
or usenew SimpleXmlElement('http://…', false, true)
. And really no offense, but given that I have already given you an almost identical solution in the comments to When extracting artist name from XML file only 1 record shows I strongly suggest you try to understand what is happening there instead of just copy and pasting.问题:
速率限制。我对这个问题的评论:
<块引用>
请注意每次执行此脚本时会生成多少网络流量,并进行相应的缓存。如果您在一天内执行得太频繁或太多次,则很可能会受到速率限制(并且 API 速率限制通常比人们想象的要低得多)。
即使您只是“测试”,或者您和“少数人”使用此功能,每个请求都会向 ws.audioscrobbler.com 发出 40 个自动请求!他们不会对此感到高兴,因为他们看起来很聪明,所以他们已经禁止了这种流量。
当我运行此脚本时,ws.audioscrobbler.com 提供第一个结果(艺术家:Adele),但显然会在经过一段时间后对许多后续请求发出请求失败的警告由于速率限制。
补救措施:
检查 ws.audioscrobbler.com 的 API 是否具有多艺术家查询。这样您就可以通过一个请求获得多位艺术家。
创建一个管理器界面,可以一次获取并缓存一位艺术家的结果。然后,在需要更新时执行此过程,并在其他时间使用缓存的结果。
无论您使用哪种方法,缓存,缓存,缓存!
向内部 foreach 提供的参数错误。
file_get_contents
返回一个字符串。尽管内容是 XML,但您还没有将其加载到 XML 解析器中。您需要先执行此操作,然后才能对其进行迭代。Problems:
Rate limiting. My comment from the question:
Even if you are just "testing", or you and a "few other people" use this, every single request makes 40 automated requests to ws.audioscrobbler.com! They are not going to be happy about this, and since it appears they are smart, they have banned this kind of traffic.
When I run this script, ws.audioscrobbler.com serves up the first result (Artist: Adele), but gives request-failed warnings on many subsequent requests until some time period has passed, obviously due to a rate limit.
Remedies:
Check if the API for ws.audioscrobbler.com has a multiple-artist query. This would allow you to get multiple artists with one request.
Create a manager interface that can get AND CACHE results for one artist at a time. Then, perform this process when you need updates and use the cached results all other times.
Regardless of which method you use, cache, cache, cache!
Wrong argument supplied to the inner foreach.
file_get_contents
returns a string. Even though the contents are XML, you haven't loaded it into an XML parser. You need to do that before you can iterate on it.