Home >Backend Development >PHP Tutorial >How to Make SOAP Calls Using PHP's SoapClient Class?

How to Make SOAP Calls Using PHP's SoapClient Class?

Barbara Streisand
Barbara StreisandOriginal
2024-12-24 06:16:14636browse

How to Make SOAP Calls Using PHP's SoapClient Class?

Creating SOAP Calls with PHP's SoapClient Class

When working with SOAP services as a client, structuring the proper SOAP call can be challenging. Using PHP's SoapClient class to make such calls can be daunting, especially for those less familiar with object-oriented coding.

Let's take the example of a SOAP function called "FirstFunction" that expects a "Contact" object as one of its parameters. The Contact object possesses the properties "id" and "name." Additionally, it requires a description and an amount.

Creating the SoapClient Object

Begin by referencing the WSDL file to establish a SoapClient object:

$client = new SoapClient("http://example.com/webservices?wsdl");

Structuring the Parameters

To create the "Contact" object, utilize the following code:

$contact = new Contact(100, "John");

Next, define the remaining parameters:

$description = "Barrel of Oil";
$amount = 500;

Combine these parameters into an array:

$params = array(
  "Contact" => $contact,
  "description" => $description,
  "amount" => $amount
);

Invoking the SOAP Call

Finally, invoke the SOAP call using the SoapClient's __soapCall method:

$response = $client->__soapCall("FirstFunction", array($params));

This complete code should successfully execute the desired SOAP call:

id = $id;
        $this->name = $name;
    }
}

$client = new SoapClient("http://example.com/webservices?wsdl");

$contact = new Contact(100, "John");
$description = "Barrel of Oil";
$amount = 500;

$params = array(
  "Contact" => $contact,
  "description" => $description,
  "amount" => $amount
);

$response = $client->__soapCall("FirstFunction", array($params));

var_dump($response);

?>

The above is the detailed content of How to Make SOAP Calls Using PHP's SoapClient Class?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn