Jump to content

Newbie with SimpleXml... need help


Guest

Recommended Posts

Hello,

 

i wrote a really simple script that gets information of a band from last.fm using SimpleXml

 

here is an example of a working script:

Quote
<?php

$request = "Crass";

 

ob_start();

 

echo rawurldecode("http://ws.audioscrobbler.com/2.0/?method=artist.getinfo&artist=$request&api_key=[redacted]");

 

$url = ob_get_clean();

 

if (!$xmlObj = simplexml_load_file($url)) { 

        echo "Error reading XML file";

} else {

 

foreach($xmlObj as $artist) {

        ob_start();

        $artist->name;

        $name = ob_get_clean();

 

 

        ob_start();

        echo $artist->image[3];

        $image = ob_get_clean();

 

 

        ob_start();

        echo $artist->bio->content;

        $bio = ob_get_clean();

}

}

 

echo $bio;

?>

 

the only problem is that when the $request is different and the band doesn't exist, it gives me an error

 

i want the script to stop and not display anything if the band is not found

 

exemple, this script

<?php
$request = "ThisBandDoesNotExist";

ob_start();

echo rawurldecode("http://ws.audioscrobbler.com/2.0/?method=artist.getinfo&artist=$request&api_key=[redacted]");

$url = ob_get_clean();

if (!$xmlObj = simplexml_load_file($url)) {  
        echo "Error reading XML file";
} else {

foreach($xmlObj as $artist) {
        ob_start();
        $artist->name;
        $name = ob_get_clean();


        ob_start();
        echo $artist->image[3];
        $image = ob_get_clean();


        ob_start();
        echo $artist->bio->content;
        $bio = ob_get_clean();
}
}

echo $bio;
?>
 

 

will give this error:

Warning: simplexml_load_file(http://ws.audioscrobbler.com/2.0/?method=artist.getinfo&artist=ThisBandDoesNotExist&api_key=[redacted]) [function.simplexml-load-file]: failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request in /var/alternc/html/a/[redacted]/xml1.php on line 9

Warning: simplexml_load_file() [function.simplexml-load-file]: I/O warning : failed to load external entity "http://ws.audioscrobbler.com/2.0/?method=artist.getinfo&artist=ThisBandDoesNotExist&api_key=[redacted]" in /var/alternc/html/a/[redacted]/xml1.php on line 9
Error reading XML file
 

 

thanks for helping me !!

Link to comment
https://forums.phpfreaks.com/topic/202474-newbie-with-simplexml-need-help/
Share on other sites

$artist = $_GET['artist'];

$url = 'http://ws.audioscrobbler.com/2.0/?method=artist.getinfo&'
       . 'artist=' . $artist . '&api_key=097a7ab0f5b56a894eb7a25e1c51b0da';
$headers = get_headers($url);
if (400 >= (int) substr($headers[0], 9, 3)) {
  echo 'Artist: ', $artist, ' does not exist.';
  exit(0);
}

$xml = simplexml_load_file($url);
//

thanks for your reply !

 

hmmm i tryed to put this with the rest of my script but when i try with an artist that exist it gives me a blank page

 

<?php
ob_start();
$artist = "contravene";
$url = 'http://ws.audioscrobbler.com/2.0/?method=artist.getinfo&'
       . 'artist=' . $artist . '&api_key=[redacted]';
$headers = get_headers($url);
if (400 >= (int) substr($headers[0], 9, 3)) {
//  echo 'Artist: ', $artist, ' does not exist.';
  exit(0);
} else {


ob_start();

$xml = simplexml_load_file($url);
echo rawurldecode($url);
$url = ob_get_clean();

foreach($xmlObj as $artist) {
        ob_start();
        $artist->name;
        $name = ob_get_clean();


        ob_start();
        echo $artist->image[3];
        $image = ob_get_clean();


        ob_start();
        echo $artist->bio->content;
        $bio = ob_get_clean();
}
echo $bio;
}
?>
 

 

what am i doing wrong ?

i said i'm a newbie in the post title

 

i started this script based on a part of code i found on internet... i'm doing the best i can

 

...but this doesn't solve my problem, i'm still getting a blank page while trying to make a request to a page that is supposed to give results, like this one:

[redacted]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.