XMLRPC中的fopen需要返回错误而不死

发布于 2024-10-09 11:21:21 字数 858 浏览 5 评论 0原文

所以我有一个在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清风不识月 2024-10-16 11:21:21
if(!($this->fh = fopen($this->log_file, 'a'))) {
    return "Can't open log file: ".$this->log_file;
}
// if you get here, $this->fh contains a file handle
if(!($this->fh = fopen($this->log_file, 'a'))) {
    return "Can't open log file: ".$this->log_file;
}
// if you get here, $this->fh contains a file handle
千里故人稀 2024-10-16 11:21:21

正如您推测的那样,使用 return 并没有什么问题,但是您需要确保在调用函数中处理此行为。

要确定 fopen 是否成功,您可以根据示例内联比较返回值,或者在文件句柄上使用 is_resource 函数。

fopen 返回:

if($this->fh = fopen($this->log_file, 'a')) {
    // Everything is fine.

}
else {
    // Error condition...
    return "Can't open log file: ".$this->log_file;
}

is_resource

$this->fh = fopen($this->log_file, 'a');
if(is_resource($this->fh)) {
    // Everything is fine...

}
else {
    // Error condition...
    return "Can't open log file: ".$this->log_file;
}

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:

if($this->fh = fopen($this->log_file, 'a')) {
    // Everything is fine.

}
else {
    // Error condition...
    return "Can't open log file: ".$this->log_file;
}

is_resource:

$this->fh = fopen($this->log_file, 'a');
if(is_resource($this->fh)) {
    // Everything is fine...

}
else {
    // Error condition...
    return "Can't open log file: ".$this->log_file;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文