Page 1 of 1

Suppress Warnings ??

Posted: Thu Jun 12, 2008 4:08 pm
by myk
I'm wondering how to suppress warnings similar to how the @ suppresses exceptions. If these are one and the same in this context please let me know. Here's an example:

Code: Select all

 
$v = new SoapClient($uri.'?WSDL');
 
if for some reason the SoapClient function can't find the given uri, it fails with a message thus:
Warning: SoapClient::SoapClient() [function.SoapClient-SoapClient]: php_network_getaddresses: getaddrinfo failed: No such host is known.

and then the script dies.

I need something that will catch this and continue. I've tried the following to no avail:

Code: Select all

 
@$v = new SoapClient($uri.'?WSDL');
$v = @new SoapClient($uri.'?WSDL');
$v = new @SoapClient($uri.'?WSDL');
 
Here's a bit more code that may help show what I'm doing:

Code: Select all

 
function getSOAPClient($uri){
     $v = new SoapClient($uri.'?WSDL');
     if(is_object($v)){#,array("exceptions" => 0));
            return($v);
     }else{
            throw new Exception("Can't create SOAP interface to $uri");
     }
}
function getAPI($uri){
     $r='';
     try{
            $pos=strpos($uri,'https://');
            if($pos!==false){
                $v = getSOAPClient($uri);
                $r.=var_dump($v->__getFunctions());
                $r.=var_dump($v->__getTypes());
            }
     }catch(Exception $e){
            echo $e->getMessage;
     }
     return($r);     
}
 

Re: Suppress Warnings ??

Posted: Thu Jun 12, 2008 4:11 pm
by Eran
Maybe you should use a different soap client, one that throws an exception before triggering a PHP error.

Re: Suppress Warnings ??

Posted: Thu Jun 12, 2008 4:23 pm
by myk
I'm just using what's available in PHP 5.2.5. Do you have a specific recommendation?

Re: Suppress Warnings ??

Posted: Thu Jun 12, 2008 4:32 pm
by Eran
I could have sworn the Zend Framework had one, but they only have XML-RPC...
You could try checking if the URL is valid before handing it to the SoapClient - http://www.woyano.com/view/130/Check-if ... sts-in-PHP

Re: Suppress Warnings ??

Posted: Thu Jun 12, 2008 4:45 pm
by myk
sure, but that's not really the issue, this sort of thing happens all over, sql queries, eval code, etc... What I need is for the php engine to keep going and let me handle the warning problem. The @ operator doesn't work in some of these cases. Is there something else out there?

Re: Suppress Warnings ??

Posted: Thu Jun 12, 2008 4:48 pm
by Eran
first of all, eval = evil... try to avoid it
second, have you looked into this? http://www.php.net/set_error_handler

Re: Suppress Warnings ??

Posted: Thu Jun 12, 2008 10:22 pm
by Ollie Saunders
You could just

Code: Select all

$priorErrorReporting = error_reporting(0); // disable
// stuff with soap
error_reporting($priorErrorReporting); // restore

Re: Suppress Warnings ??

Posted: Fri Jun 13, 2008 9:59 am
by myk
So I tried wrapping the code with both set_error_handler/restore_error_handler and the error_reporting idea and I was able to get some output from my own error handler, the SoapClient call still killed the script. I found a bug with similar behavior described, so I filed a bug to reopen it. For now I can get by with array_shift()ing the offending uri off my array.

thanks for your help.

myk

Re: Suppress Warnings ??

Posted: Fri Jun 13, 2008 11:25 am
by RobertGonzalez
I am not sure that you can suppress an error on a constructor of an object. Same thing with throwing exceptions. Is there a way that you can construct the object without a param and set the URI after construction? Perhaps then you can add a check to it, throw an exception if the URI is jacked and handle that with a try catch block in the app code.

Re: Suppress Warnings ??

Posted: Fri Jun 13, 2008 11:53 am
by Ollie Saunders
When it says no such host is known which host it is referring to? Have you checked the contents of the variable that's going to the constructor? Once you know that you should verify that you can actually ping that host.

Also you could probably wrap the SoapClient instantiation with a test to see if the hosts exists to prevent the script dying and the error being triggered.