检查xml文件是否为空

时间:2012-12-17 19:03:29

标签: php xml error-handling xml-parsing

如果我推送一个不存在或拼写错误的字符串来运行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

1 个答案:

答案 0 :(得分:3)

如何通过添加&#39; @&#39;?

来打印警告
$xml = @simplexml_load_file($completeurl);
if(!$xml)
{
   return false;
}
相关问题