0

如果我将一个不存在或拼写错误的字符串推送到函数中,benzer($ilk_artist)我会得到错误底部。我是否推送一个有效的艺术家姓名变量$completeurl总是被定义的。所以我不能说if($completeurl)我认为我应该控制$completeurl之前是否有效simplexml_load_file($completeurl)。你知道我该怎么做吗?

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);

}

错误:

[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
4

1 回答 1

3

不通过添加'@'打印出警告怎么办?

$xml = @simplexml_load_file($completeurl);
if(!$xml)
{
   return false;
}
于 2012-12-18T00:52:39.600 回答