Jump to content

Newbie with SimpleXml... need help


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:

<?php

$request = "Crass";

 

ob_start();

 

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

 

$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=097a7ab0f5b56a894eb7a25e1c51b0da");

$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=097a7ab0f5b56a894eb7a25e1c51b0da) [function.simplexml-load-file]: failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request in /var/alternc/html/a/anarchoi/anarchopunk/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=097a7ab0f5b56a894eb7a25e1c51b0da" in /var/alternc/html/a/anarchoi/anarchopunk/xml1.php on line 9
Error reading XML file

 

thanks for helping me !!

Link to comment
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);
//

Link to comment
Share on other sites

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=097a7ab0f5b56a894eb7a25e1c51b0da';
$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 ?

Link to comment
Share on other sites

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:

http://ws.audioscrobbler.com/2.0/?method=artist.getinfo&artist=crass&api_key=097a7ab0f5b56a894eb7a25e1c51b0da

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.