Suppress Warnings ??

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
myk
Forum Newbie
Posts: 4
Joined: Thu Jun 12, 2008 4:00 pm

Suppress Warnings ??

Post 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);     
}
 
Last edited by Weirdan on Thu Jun 12, 2008 5:51 pm, edited 1 time in total.
Reason: php tags
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Suppress Warnings ??

Post by Eran »

Maybe you should use a different soap client, one that throws an exception before triggering a PHP error.
myk
Forum Newbie
Posts: 4
Joined: Thu Jun 12, 2008 4:00 pm

Re: Suppress Warnings ??

Post by myk »

I'm just using what's available in PHP 5.2.5. Do you have a specific recommendation?
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Suppress Warnings ??

Post 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
myk
Forum Newbie
Posts: 4
Joined: Thu Jun 12, 2008 4:00 pm

Re: Suppress Warnings ??

Post 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?
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Suppress Warnings ??

Post by Eran »

first of all, eval = evil... try to avoid it
second, have you looked into this? http://www.php.net/set_error_handler
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Re: Suppress Warnings ??

Post by Ollie Saunders »

You could just

Code: Select all

$priorErrorReporting = error_reporting(0); // disable
// stuff with soap
error_reporting($priorErrorReporting); // restore
myk
Forum Newbie
Posts: 4
Joined: Thu Jun 12, 2008 4:00 pm

Re: Suppress Warnings ??

Post 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
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Suppress Warnings ??

Post 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.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Re: Suppress Warnings ??

Post 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.
Post Reply