mcan
mcan

Reputation: 2082

Checking whether xml files are empty or not

If i push a string which doesn't exist or misspelled to function benzer($ilk_artist) i am getting errors bottom. Whether i push a valid artist name or not variable $completeurl is always defined. So i can't put if($completeurl) I think i should control whether $completeurl is valid before simplexml_load_file($completeurl). Do you have an idea that how i can do it?

function benzer($ilk_artist)
{
    $completeurl = 'http://ws.audioscrobbler.com/2.0/?method=artist.getsimilar&artist='.trim($ilk_artist).'&api_key='.LASTFM_APP_ID;
    $completeurl = urlencode($completeurl);
    $xml = simplexml_load_file($completeurl);
    if(!$xml)
    {
        return false;
    }
    $artists = $xml->similarartists->artist;
    $length = count($artists); 
    for ($i = 0; $i < $length; $i++) {
        $artistname[$i] = $artists[$i]->name;
    }

    return simplexml_kurtul($artistname);

}

errors:

[17-Dec-2012 11:43:33] PHP Warning:  simplexml_load_file(http://ws.audioscrobbler.com/2.0/?method=artist.getsimilar&amp;artist=tgyu+6uh6n&amp;api_key=APIKEY) [<a href='function.simplexml-load-file'>function.simplexml-load-file</a>]: failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request
 in /home6/.../public_html/.../functions/fns.php on line 102
[17-Dec-2012 11:43:33] PHP Warning:  simplexml_load_file() [<a href='function.simplexml-load-file'>function.simplexml-load-file</a>]: I/O warning : failed to load external entity &quot;http%3A%2F%2Fws.audioscrobbler.com%2F2.0%2F%3Fmethod%3Dartist.getsimilar%26artist%3Dtgyu+6uh6n%26api_key=APIKEY0&quot; in /home6/.../public_html/.../functions/fns.php on line 102

Upvotes: 0

Views: 587

Answers (1)

Diego Pino
Diego Pino

Reputation: 11576

What about not printing out the warnings by adding '@'?

$xml = @simplexml_load_file($completeurl);
if(!$xml)
{
   return false;
}

Upvotes: 3

Related Questions