gpt4 book ai didi

php - 检查xml文件是否为空

转载 作者:行者123 更新时间:2023-12-03 08:08:09 27 4
gpt4 key购买 nike

如果我将不存在的字符串或拼写错误的字符串推送到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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com