2009-08-20 113 views
17

我使用下面的代码:SimpleXML的错误处理PHP

function GetTwitterAvatar($username){ 
$xml = simplexml_load_file("http://twitter.com/users/".$username.".xml"); 
$imgurl = $xml->profile_image_url; 
return $imgurl; 
} 

function GetTwitterAPILimit($username, $password){ 
$xml = simplexml_load_file("http://$username:[email protected]/account/rate_limit_status.xml"); 
$left = $xml->{"remaining-hits"}; 
$total = $xml->{"hourly-limit"}; 
return $left."/".$total; 
} 

遇到这些错误时流无法连接:

Warning: simplexml_load_file(http://twitter.com/users/****.xml) [function.simplexml-load-file]: failed to open stream: HTTP request failed! HTTP/1.0 503 Service Unavailable 

Warning: simplexml_load_file() [function.simplexml-load-file]: I/O warning : failed to load external entity "http://twitter.com/users/****.xml" 

Warning: simplexml_load_file(http://[email protected]/account/rate_limit_status.xml) [function.simplexml-load-file]: failed to open stream: HTTP request failed! HTTP/1.0 503 Service Unavailable 

Warning: simplexml_load_file() [function.simplexml-load-file]: I/O warning : failed to load external entity "http://***:***@twitter.com/account/rate_limit_status.xml" 

我如何处理这些错误,所以我可以显示用户而不是上面显示的内容?

+0

考虑更改所选择的答案吗?问题问:“我如何处理这些错误?“所选的答案只是告诉你如何避免不必要。 – 2017-04-26 03:25:29

回答

42

我认为这是一个更好的办法

$use_errors = libxml_use_internal_errors(true); 
$xml = simplexml_load_file($url); 
if (false === $xml) { 
    // throw new Exception("Cannot load xml source.\n"); 
} 
libxml_clear_errors(); 
libxml_use_internal_errors($use_errors); 

更多信息:http://php.net/manual/en/function.libxml-use-internal-errors.php

+1

只有一个回答问题。将它与'libxml_get_errors()'或'libxml_get_last_error()'一起使用以获取错误消息。 – Znarkus 2011-04-07 17:14:34

+1

但是,这无法获取由simplexml_load_file转储的所有警告消息。 – 2012-09-10 16:30:14

+0

我试过这个,但得到了一个警告消息,所以我只是做$ xml = @simplexml_load_file($ url);并且没有警告并按预期处理错误 – bestprogrammerintheworld 2016-03-09 09:17:35

0

该文档说明,如果出现错误,simplexml_load_file将返回FALSE。所以,你可以结合使用 “闭嘴” 操作符(@)与条件语句:(!使用simplexml_load_file($文件)== 假)

if (@simplexml_load_file($file)) 
{ 
    // continue 
} 
else 
{ 
    echo 'Error!'; 
} 
+17

不是闭嘴不闭嘴,请! – 2009-08-20 16:21:21

+0

所以这样的事情? function GetTwitterAvatar($ username){@simplexml_load_file(”http: //twitter.com/users /".$用户名 “XML”。)){$ XML =使用simplexml_load_file( “http://twitter.com/users/” $用户名 “的.xml”);。。。 $ imgUrl的= $ XML-> profile_image_url; 回报$ imgUrl的; }其他{ 回报 '错误';} } 你 – mrpatg 2009-08-20 16:23:55

+2

可以做到这一点,而无需调用使用simplexml_load_file两次:$ XML = @simplexml_load_file( “...”);如果($ xml){...} else {return'Error';} – 2009-08-20 16:26:55

-1

如果{// 继续}其他{ echo'错误!'; }

Twitter is down,也许?

+0

Twitter已关闭! – Znarkus 2011-04-07 17:07:16

+5

-1,你并没有真正处理错误。 – greg0ire 2012-02-21 09:06:24

14

我发现在php documentation一个很好的例子。

因此,代码为:

libxml_use_internal_errors(true); 
$sxe = simplexml_load_string("<?xml version='1.0'><broken><xml></broken>"); 
if (false === $sxe) { 
    echo "Failed loading XML\n"; 
    foreach(libxml_get_errors() as $error) { 
     echo "\t", $error->message; 
    } 
} 

和输出,正如我们/我所料:

无法加载XML

Blank needed here 
parsing XML declaration: '?>' expected 
Opening and ending tag mismatch: xml line 1 and broken 
Premature end of data in tag broken line 1 
+2

但是,这不能获取由simplexml_load_string转储的所有警告消息。 – 2012-09-10 16:28:43

6

如果您看看马NUAL,有一个选项参数:

SimpleXMLElement simplexml_load_file (string $filename [, string $class_name = "SimpleXMLElement" [, int $options = 0 [, string $ns = "" [, bool $is_prefix = false ]]]]) 

选项列表,请访问:http://www.php.net/manual/en/libxml.constants.php

这是为了抑制警告解析警告正确的方法:

$xml = simplexml_load_file('file.xml', 'SimpleXMLElement', LIBXML_NOWARNING); 
+0

没有为我工作,仍然在PHP 5.4.x中生成E_WARNING – 2015-12-29 19:23:01

2

这是一个老问题,但今天仍然有用。

使用oop SimpleXMLElment处理异常的正确方法就像这样。

libxml_use_internal_errors(TRUE); // this turns off spitting errors on your screen 
try { 
    $xml = new SimpleXMLElement($xmlStringOfData); 
} catch (Exception $e) { 
    // Do something with the exception, or ignore it. 
} 
1

我的小码:

try { 
    libxml_use_internal_errors(TRUE); 
    $xml = new SimpleXMLElement($xmlString); 
    echo '<pre>'.htmlspecialchars($xml->asXML()).'</pre>'; 
} catch (Exception $e) { 
    echo 'Caught exception: ' . $e->getMessage() . chr(10); 
    echo 'Failed loading XML: ' . chr(10); 
    foreach(libxml_get_errors() as $error) { 
     echo '- ' . $error->message; 
    } 
} 

结果例如:

Caught exception: String could not be parsed as XML 
Failed loading XML: 
- Opening and ending tag mismatch: Body line 3 and Bod-y