Guest Posted May 21, 2010 Share Posted May 21, 2010 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 More sharing options...
ignace Posted May 21, 2010 Share Posted May 21, 2010 $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 https://forums.phpfreaks.com/topic/202474-newbie-with-simplexml-need-help/#findComment-1061515 Share on other sites More sharing options...
Guest Posted May 21, 2010 Share Posted May 21, 2010 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 ? Link to comment https://forums.phpfreaks.com/topic/202474-newbie-with-simplexml-need-help/#findComment-1061533 Share on other sites More sharing options...
ignace Posted May 21, 2010 Share Posted May 21, 2010 Who did you learn to program like: ob_start(); echo 'blabla'; $var = ob_get_clean(); Seriously, this is horrible. Not to mention the serious overhead. While it's only: $var = 'blabla'; Link to comment https://forums.phpfreaks.com/topic/202474-newbie-with-simplexml-need-help/#findComment-1061538 Share on other sites More sharing options...
Guest Posted May 21, 2010 Share Posted May 21, 2010 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] Link to comment https://forums.phpfreaks.com/topic/202474-newbie-with-simplexml-need-help/#findComment-1061559 Share on other sites More sharing options...
ignace Posted May 21, 2010 Share Posted May 21, 2010 Typo if (400 >= should be if (400 <= Link to comment https://forums.phpfreaks.com/topic/202474-newbie-with-simplexml-need-help/#findComment-1061613 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.