php - 检查xml文件是否为空

标签 php xml error-handling xml-parsing

如果我将不存在的字符串或拼写错误的字符串推送到benzer($ilk_artist)函数,则会出现错误。始终定义是否推送有效的艺术家姓名。所以我不能把$completeurl放在if($completeurl)之前,我应该控制$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

最佳答案

如何不通过添加“@”来打印警告?

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

关于php - 检查xml文件是否为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13920426/

相关文章:

python - 生成/写入 XML 文件的标准方式

Mysql错误信息:"#1044 - Access denied for user ' root'@'localhost' to database 'information_schema'"当试图删除 TableView

symfony - Symfony2 : Email error not send

php - Laravel 模型上 Trait 中的范围查询

PHP 发送的电子邮件有 =0A=0A 而不是新行

php - 当我导航到其他页面时, session 不会保留/销毁

java - Jackson XML 解码和抽象类

php - Mysqli LIKE 无法使用撇号?

xml - 将 Scala 对象转换为 XML

python - 为什么jupyter即将出现名称未定义