When working with SOAP web services, it's essential to understand how to structure and send data in a format compatible with the service. This article demonstrates how to use the SoapClient class to make a SOAP call, specifically focusing on handling structured data.
In the scenario presented, our goal is to make a call to the "FirstFunction" function of a web service using the following structured data:
Create a Contact Class (if necessary)
For this example, we assume you have a PHP class named Contact with properties id and name. If you don't have this class, create it as follows:
class Contact { public $id; public $name; }
Utilize SoapClient for SOAP Function Call
a. Instantiate the SoapClient with the web service WSDL URL:
$client = new SoapClient("http://example.com/webservices?wsdl");
b. Create an instance of the Contact class with the desired data:
$contact = new Contact(); $contact->id = 100; $contact->name = "John";
c. Set request parameters in the correct structure:
$params = array( "Contact" => $contact, "description" => "Barrel of Oil", "amount" => 500, );
d. Invoke the SOAP method:
$response = $client->__soapCall("Function1", array($params));
Note: Make sure to replace "Function1" with the actual name of your SOAP function.
By following these steps, you can successfully make SOAP calls using the SoapClient class and pass structured data to the web service as expected.
The above is the detailed content of How to Send Structured Data with PHP's SoapClient: A Step-by-Step Guide?. For more information, please follow other related articles on the PHP Chinese website!