PHP simplexml_load_file - 捕获文件错误
是否可以捕获 simplexml 文件错误?我正在连接到一个有时会失败的网络服务,如果它返回一些http错误或类似的错误,我需要让系统跳过一个文件。
Is it possible to catch simplexml file errors? I'm connecting to a webservice that sometimes fails, and I need to make the system skip a file if it returns some http error or something similar.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
使用
@
简直就是肮脏的。如果你看一下手册,有一个选项参数:
所有选项列表都可以在这里找到: http://www.php.net/manual/en/libxml.constants.php
这是抑制警告的正确方法:
Using
@
is just plain dirty.If you look at the manual, there is an options parameter:
All option list is available here: http://www.php.net/manual/en/libxml.constants.php
This is the correct way to suppress warnings:
你在谈论两件不同的事情。 HTTP 错误与 XML 文件是否有效无关,因此您需要考虑两个不同的错误处理区域。
您可以利用 libxml_use_internal_errors()抑制任何 XML 解析错误,然后手动检查它们(使用 libxml_get_errors()) 在每次解析操作之后。我建议这样做,因为您的脚本不会产生大量
E_WARNING
消息,但您仍然会找到无效的 XML 文件。至于 HTTP 错误,处理这些错误将取决于您如何连接到 Web 服务并检索数据。
You're talking about two different things. HTTP errors will have nothing to do with whether an XML file is valid, so you're looking at two separate areas of error handling.
You can take advantage of libxml_use_internal_errors() to suppress any XML parsing errors, and then check for them manually (using libxml_get_errors()) after each parse operation. I'd suggest doing it this way, as your scripts won't produce a ton of
E_WARNING
messages, but you'll still find the invalid XML files.As for HTTP errors, handling those will depend on how you're connecting to the webservice and retrieving the data.
如果您对 Web 服务失败时的错误报告或日志记录不感兴趣,您可以使用错误抑制运算符:
但这只是一个简单的 hack。更强大的解决方案是使用 cURL 加载 XML 文件,记录任何失败的请求,解析任何 XML使用
simplexml_load_string
返回的文档,记录任何 XML 解析错误,然后对有效的 XML 执行一些操作。If you're not interested in error reporting or logging when the webservice fails you can use the error supression operator:
But this is a simple hack. A more robust solution would be to load the XML file with cURL, log any failed requests, parse any XML document returned with
simplexml_load_string
, log any XML parse errors and then do some stuff with the valid XML.出错时,您的 simplexml_load_file 应返回 false。
因此,做一些像这样简单的事情:
是检测错误的一种方法。
On error, your simplexml_load_file should return false.
So doing somethign as simple as this:
Is one way to detect errors.
您可以在 PHP 中设置一个错误处理程序,以在出现任何 PHP 错误时抛出异常:(示例和更多文档可在此处找到:PHP.net)
You can set up an error handler within PHP to throw an Exception upon any PHP Errors: (Example and further documentation found here: PHP.net)
另一种选择是使用 libxml_use_internal_errors() 函数来捕获错误。然后可以使用 libxml_get_errors() 函数检索错误。如果您想检查具体错误是什么,这将允许您循环遍历它们。如果您确实使用此方法,则需要确保在完成错误后将其从内存中清除,这样它们就不会浪费您的内存空间。
这是一个例子:
另一个例子实际上利用了我们捕获的错误:
Another option is to use the
libxml_use_internal_errors()
function to capture the errors. The errors can then be retrieved using thelibxml_get_errors()
function. This will allow you to loop through them if you want to check what the specific errors are. If you do use this method, you will want to make sure that you clear the errors from memory when you are done with them so they are not wasting your memory space.Here is an example:
Another example actually making use of the errors we captured: