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 wil开发者_高级运维l 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;
}
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
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;
}
精彩评论