XMLRPC中的fopen需要返回错误而不死
所以我有一个在 Zend PHP 中开发的 XMLRPC,我试图返回错误消息而不是使用 die()。
这就是我所拥有的:
$this->fh = fopen($this->log_file, 'a')
or die("Can't open log file: ".$this->log_file);
这样的事情可能吗? (伪代码)
if($this->fh = fopen($this->log_file, 'a')) {
return "Can't open log file: ".$this->log_file;
}
这可能就在我眼皮子底下,我猜
解决方案:
对于 XMLRPC 进程,E_WARNING 将终止/崩溃该进程。拥有 XMLRPC 使用警告消息进行响应 在函数前面使用 @ 符号来抑制 警告。 http://php.net/manual/en/function.fopen.php #错误/异常
// If the open fails,
// an error of level E_WARNING is generated.
// You may use @ to suppress this warning.
if(!($this->fh = @fopen($this->log_file, 'a'))) {
return "Can't open log file: ".$this->log_file;
}
So I have a XMLRPC developed in Zend PHP and I'm trying to return the error message instead of using the die().
Here is what I have:
$this->fh = fopen($this->log_file, 'a')
or die("Can't open log file: ".$this->log_file);
Is something like this possible? (Pseudo code)
if($this->fh = fopen($this->log_file, 'a')) {
return "Can't open log file: ".$this->log_file;
}
It's probably right under my nose just having a brain fart I guess
Solution:
For the XMLRPC process the E_WARNING will kill/crash the process. To have the XMLRPC
respond with the warning message use the @ symbol in front of the function to suppress
the warning. http://php.net/manual/en/function.fopen.php #Errors/Exceptions
// If the open fails,
// an error of level E_WARNING is generated.
// You may use @ to suppress this warning.
if(!($this->fh = @fopen($this->log_file, 'a'))) {
return "Can't open log file: ".$this->log_file;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如您推测的那样,使用 return 并没有什么问题,但是您需要确保在调用函数中处理此行为。
要确定 fopen 是否成功,您可以根据示例内联比较返回值,或者在文件句柄上使用 is_resource 函数。
fopen 返回:
is_resource:
There's nothing wrong with using the return as you speculate, however you'll need to ensure that you handle this behaviour within the calling function.
To determine if the fopen was successful, you can either compare the return value inline as per your example or use the is_resource function on the file handle.
fopen return:
is_resource: