Often, non-Object-Oriented programmers encounter challenges when integrating SOAP functionality into their PHP scripts. Despite having a WSDL file, they may struggle with the correct syntax for invoking SOAP calls.
To interact with a SOAP service as a client, you can utilize the SoapClient class. Begin by creating an instance of SoapClient with the WSDL file's URL:
$client = new SoapClient("http://example.com/webservices?wsdl");
To retrieve information about available functions and types, use:
var_dump($client->__getFunctions()); var_dump($client->__getTypes());
Now, let's focus on constructing the SOAP call itself. Say you want to invoke the "Function1" function with the following data:
The WSDL document indicates that Contact is a complex type with two properties: id and name.
To make the SOAP call with your desired data, you'll need to use the following syntax:
$params = array( "Contact" => new Contact(100, "John"), "description" => "Barrel of Oil", "amount" => 500, ); $response = $client->__soapCall("Function1", array($params));
This code creates a new instance of the Contact class with the specified ID and name. It then assembles the request parameters into the $params array and invokes the SOAP call using __soapCall.
The response from the SOAP service will be stored in the $response variable. You can inspect the response to obtain the results of the call.
The above is the detailed content of How to Use PHP's SoapClient to Integrate with SOAP Web Services?. For more information, please follow other related articles on the PHP Chinese website!