2012-12-17 83 views
0

如果我推送一個不存在的字符串或拼寫錯誤的函數爲benzer($ilk_artist),我得到的錯誤是底部。我是否會推送一個有效的藝術家名稱或變量$completeurl總是被定義。所以我不能把if($completeurl)我認爲我應該控制是否$completeurl之前simplexml_load_file($completeurl)有效。你有一個想法,我該如何做到這一點?檢查xml文件是否爲空

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 

回答

3

什麼不加 '@' 打印出警告?

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

由於解決方案的工作,而不是upvoting的答案,你可以標記爲接受? http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work –