SimpleXML的错误处理PHP(simplexml error handling php)

2019-06-18 16:10发布

我使用下面的代码:

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:$password@twitter.com/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://...@twitter.com/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"

我该如何处理这些错误,所以我可以显示一个用户友好的消息,而不是上面显示?

Answer 1:

我已经找到了一个很好的例子PHP文件 。

因此,代码为:

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 


Answer 2:

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

$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



Answer 3:

如果你看一下说明书,有一个选项参数:

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);


Answer 4:

这是一个老问题,但今天仍然具有现实意义。

使用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.
}


Answer 5:

我的一些代码:

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


Answer 6:

文档说,在一个错误的情况下,使用simplexml_load_file返回FALSE。 所以,你可以结合使用“闭嘴”操作符(@)与条件语句:

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


Answer 7:

如果(使用simplexml_load_file($文件)==假的!){//继续}其他{回声 '错误!'; }

而Twitter的宕机 ,也许?



文章来源: simplexml error handling php