如何对simplexml_load_file进行错误检查? [英] How to put an error check for simplexml_load_file?

查看:170
本文介绍了如何对simplexml_load_file进行错误检查?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Tumbrl API将几个帖子加载到我的主页上。
我有以下调用:

  $ xml = simplexml_load_file($ request_url); 

有时它加载得很好,但其他时候我会得到这个输出:

 警告:simplexml_load_file(http://mysite.tumblr.com/api/read?type=post&start=0&num=10&type=photo) [function.simplexml-load-file]:无法打开流:HTTP请求失败! HTTP / 1.1 503服务暂时不可用在线624上的mysite.com/html/index.php 

警告:simplexml_load_file()[function.simplexml-load-file]:I / O警告:未能加载外部实体http://mysite.tumblr.com/api/read?type=post&start=0&num=10&type=photo在第624行的mysite.com/html/index.php

我应该执行什么样的错误检查,以避免在页面上显示警告?

解决方案

检查此函数失败的方法是检查 false 返回值。 / p>

simplexml_load_file也会在失败时触发旧式的PHP警告。如果您有警告打开,但是您想以任何原因阻止警告,您可以使用 @



所以,你可以使用

  $ element = @simplexml_load_file($ file); 
if($ element === false){
// error!
}

请注意,警告可能是可能出现问题的指标。如果您正在阅读文件,但您不确定该文件是否存在,请注意避免出现安全问题?例如,用户是否可以读取系统上的任何文件?



更新2014年3月



作为一个警告,我应该补充说,使用@运算符来抑制错误是一个坏主意。如果有替代方案,你应该使用它。如果没有其他选择,您应该尝试将@所覆盖的部分限制在尽可能小的部分。



原因是@覆盖您在全局设置的任何错误处理设置(与代码中设置 error_reporting()相同)。因此,如果您设置了PHP来记录错误而不是显示它们,例如,此错误甚至不会被记录。与打一样,您也可以轻易地遏制更多错误,包括您想知道的错误。例如,使用 @myfunction($ something)可以抑制在该函数中的任何地方发生的错误,或者依次对函数进行调用的函数。在这种情况下,由于您是通过HTTP检查远程文件,通常使用@的替代方法是不理想例如本地文件,您可以使用以下内容来避免@:

  if(file_exists($ file)){
$ element = simplexml_load_file($ file);
}
else {
//错误!
}

但在您的情况下,由于它是通过HTTP调用远程文件,以上将导致两个请求,如果第一个请求成功,但第二个请求失败,您将收到错误。在你的情况下,特别限于 simplexml_load_file()的@运算符可能是最好的。


I'm using Tumbrl API to load a few posts onto my homepage. I have the following call:

$xml = simplexml_load_file($request_url);

Occasionally it loads fine but other times I get this output instead:

Warning: simplexml_load_file(http://mysite.tumblr.com/api/read?type=post&start=0&num=10&type=photo) [function.simplexml-load-file]: failed to open stream: HTTP request failed! HTTP/1.1 503 Service Temporarily Unavailable in mysite.com/html/index.php on line 624

Warning: simplexml_load_file() [function.simplexml-load-file]: I/O warning : failed to load external entity "http://mysite.tumblr.com/api/read?type=post&start=0&num=10&type=photo" in mysite.com/html/index.php on line 624

What kind of error check should I implement to avoid showing the warning on the page?

解决方案

The way to check for failure of this function is to check for a false return value.

simplexml_load_file also fires an old-style PHP warning when it fails. If you have warnings turned on but you want to suppress the warning for whatever reason, you could use @:

So, you can use

$element = @simplexml_load_file($file);
if ($element === false) {
  // error!
}

Note that the warning can be an indicator of a possible problem. If you are reading a file but you are not sure if that file exists, have you been careful to avoid a security problem? For example, are users able to read any file on your system?

Update March 2014

As a caveat, I should add that using the "@" operator to suppress an error is a bad idea in general. If there is an alternative, you should use it. If there isn't an alternative, you should try to restrict the part covered by "@" to the smallest possible part.

The reason for this is that "@" overrides any error handling settings you have set globally (in the same way as setting error_reporting() in your code). So if you've set PHP to log errors instead of display them, for example, this error won't even be logged. It's also easy to accidentally suppress more errors than you intend with "@" including errors you'd want to know about. For example, using @myfunction($something) will suppress errors that happen anywhere in that function, or in functions that function calls in turn. So this is just something to keep in mind about that operator.

In this case, because you are checking remote files over HTTP, the usual alternative to using "@" is not ideal. Eg for local files you may use the following to avoid "@":

if (file_exists($file)) {
  $element = simplexml_load_file($file);
}
else {
  // error!
}

But in your case since it is a call to a remote file over HTTP, the above would result in two requests and if the first request succeeds but the second one fails, you'd get an error. In your case an "@" operator specifically limited to your simplexml_load_file() is probably the best.

这篇关于如何对simplexml_load_file进行错误检查?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆