Instance code 1: try {$ this-soapClientObjnewSoapClient (self: URL .? Wsdl, array (connection_timeoutself: CONNECTION_TIMEOUT);} catch (Exception $ e) {thrownewException ($ e-getMessage (), $ e-getCode ());} instance Code 2 :? Phpheader (
Instance code 1: try {$ this-soapClientObj = new SoapClient (self: URL .'? Wsdl ', array ('Connection _ timeout' = self: CONNECTION_TIMEOUT);} catch (Exception $ e) {throw new Exception ($ e-getMessage (), $ e-getCode ();} instance Code 2 :? Php header ("
Instance code 1:
Try {
$ This-> soapClientObj = new SoapClient (self: URL .'? Wsdl ', array ('Connection _ timeout' => self: CONNECTION_TIMEOUT ));
} Catch (Exception $ e ){
Throw new Exception ($ e-> getMessage (), $ e-> getCode ());
}
Instance Code 2:
Header ("Content-Type: text/html; charset = UTF-8 ");
/*
* Specify the WebService path and initialize a WebService client.
*/
$ Ws = "http://www.webservicex.net/globalweather.asmx? Wsdl "; // address of the webservice
$ Client = new SoapClient ($ ws );
/*
* Obtain all methods provided by the Service referenced by the SoapClient object
*/
Echo 'open functions provided by the SOAP server :';
Echo'
';
Var_dump ($ client->__ getFunctions (); // obtain the method provided on the server
Echo "";
Echo 'Type provided by the SOAP server :';
Print_r ($ client->__ getTypes (); // obtain the data type on the server
Echo "";
Echo 'result of executing GetGUIDNode :';
// Query the weather in Beijing, China. A struct is returned.
$ Result = $ client-> getWeather (array ('cityname' => 'beijing', 'countryname' => 'China '));
Echo $ result-> GetWeatherResult; // display the result
?>
Running result:
Example of try and catch
Eg:
// Create a function that can throw an exception
Function checkNum ($ number ){
If ($ number> 1 ){
Throw new Exception ("Value must be 1 or below ");
}
Return true;
}
// Trigger an exception in the try code block
Try {
// If the exception is thrown, this text will not be shown echo 'If you see this, the number is 1 or below ';
CheckNum (2 );
} Catch (Exception $ e ){
// Capture exceptions
Echo 'message: '. $ e-> getMessage ();
}
?>
The above code will get an error similar to this:
Message: Value must be 1 or below
Example:The above code throws an exception and captures it:
- Create the checkNum () function. It checks whether the number is greater than 1. If yes, an exception is thrown.
- Call the checkNum () function in the "try" code block.
- An exception in the checkNum () function is thrown.
- The "catch" code block receives the exception and creates an object ($ e) containing the exception information ).
- Call $ e-> getMessage () from this exception object to output error messages from this exception.
However, to follow the "each throw must correspond to a catch" principle, you can set a top-level exception processor to handle missed errors.