Example 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 ());
}
Example code 2:
<?php
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 service
$client = new SoapClient ($WS);
/*
* Get all the methods provided by the service that the SoapClient object references
*/
Echo ' SOAP server provides an open function: ';
Echo ' <pre> ';
Var_dump ($client->__getfunctions ());//Get the method provided on the server
echo "
Echo ' SOAP server provides the type: ';
Print_r ($client->__gettypes ());//Get the data type on the server
echo "
Echo ' Execution of getguidnode results: ';
Find weather in Beijing, China returns a structure
$result = $client->getweather (Array (' cityname ' = ' Beijing ', ' countryname ' = ' China ');
echo $result->getweatherresult;//Display Results
?>
Operation Result:
Instance description of try and catch
eg
<?php
To create a function that throws an exception
function Checknum ($number) {
if ($number >1) {
throw new Exception ("Value must be 1 or below");
}
return true;
}
To trigger an exception in a "try" code block
try {
If the exception is a thrown, this text won't be shown Echo's if you see this and the number is 1 or below ';
Checknum (2);
}catch (Exception $e) {
Catching exceptions
Echo ' Message: '. $e->getmessage ();
}
?>
The above code will get an error like this:
Example Explanation:
The above code throws an exception and captures it:
- Create the Checknum () function. It detects if the number is greater than 1. If it is, an exception is thrown.
- Call the Checknum () function in the "Try" code block.
- The exception in the Checknum () function is thrown
- The catch code block receives the exception and creates an object ($e) that contains the exception information.
- Output the error message from this exception by calling $e->getmessage () from this exception object
However, to follow the principle that each throw must correspond to a catch, you can set up a top-level exception handler to handle the missing error.
PHP calls the Web service interface (. NET developed interface)