开发者

When parsing XML with PHP, it only shows the first record from the file, not all of them

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 fro开发者_StackOverflow中文版m 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.


The returned XML is structured like this:

<lfm>
    <images>
        <image>
        <image>
        …
        <image>

Which means you have to iterate

$xml->images->image

Example:

$lfm = simplexml_load_file('http://…');
foreach ($lfm->images->image as $image) {
    echo $image->sizes->size[4];
}

On a sidenote, there is no reason to use file_get_contents there. Either use simplexml_load_file or use new 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.


Problems:

  1. Rate limiting. My comment from the question:

    Please note how much network traffic you are generating on each execution of this script, and cache accordingly. It's quite possible you could be rate-limited if you execute too often or too many times in a day (and API rate-limits are often a lot lower than one might think).

    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:

    1. Check if the API for ws.audioscrobbler.com has a multiple-artist query. This would allow you to get multiple artists with one request.

    2. 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!

  2. 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜