Sets one or more SOAP headers included in subsequent client calls. This guide demonstrates its use in PHP code and explains when the result or side effect needs extra checks.
What It Does
Sets one or more SOAP headers included in subsequent client calls.
Syntax and Parameters
The examples below show a real call to SoapClient::__setSoapHeaders with suitable arguments. Read the function call in context: its connection, handle, input data or other prerequisites are created before use.
Requirements: SOAP extension; the corresponding WSDL or service implementation.
Example 1: Core Behavior
Requires a real trusted WSDL and deployed SOAP endpoint; operation arguments follow that contract.
<?php
$client = new SoapClient(getenv("SOAP_WSDL") ?: "https://soap.internal.test/service?wsdl", ["exceptions" => true, "trace" => false]);
$header = new SoapHeader("urn:example:service", "RequestId", bin2hex(random_bytes(8)));
$client->__setSoapHeaders($header);
echo "SOAP header configured";
Observe the returned value or changed resource state. A successful function call can still require additional checks before a higher-level operation is considered successful.
Example 2: Alternative or Failure Case
Requires a real trusted WSDL and deployed SOAP endpoint; operation arguments follow that contract.
<?php
$client = new SoapClient(getenv("SOAP_WSDL") ?: "https://soap.internal.test/account?wsdl", ["exceptions" => true, "trace" => false]);
$header = new SoapHeader("urn:example:billing", "RequestId", bin2hex(random_bytes(8)));
$client->__setSoapHeaders($header);
echo "SOAP header configured";
Compare the different inputs, environment assumptions or result checks. Do not use a transport-level success value as proof that all business-level requirements were met.
Practical Example: Applying the Rule
Requires a real trusted WSDL and deployed SOAP endpoint; operation arguments follow that contract.
<?php
$client = new SoapClient(getenv("SOAP_WSDL") ?: "https://soap.internal.test/inventory?wsdl", ["exceptions" => true, "trace" => false]);
$header = new SoapHeader("urn:example:inventory", "RequestId", bin2hex(random_bytes(8)));
$client->__setSoapHeaders($header);
echo "SOAP header configured";
In an application, convert low-level failures into controlled responses and log only non-sensitive diagnostic context.
Important Behavior and Edge Cases
Provide SoapHeader objects and avoid placing secret tokens into debug logs.
Comparison with Related Functions or Patterns
| Approach | When it differs |
|---|---|
| SoapClient::__setSoapHeaders | Sets one or more SOAP headers included in subsequent client calls. |
| Related API or pattern | __soapCall performs the actual SOAP operation. |
Common Beginner Mistakes
| Mistake | Correct approach |
|---|---|
| Exposing an unvalidated operation or assuming every WSDL uses the same arguments | Provide SoapHeader objects and avoid placing secret tokens into debug logs. |
| Substituting a related API without checking its semantics | __soapCall performs the actual SOAP operation. |
Key Takeaways
- Sets one or more SOAP headers included in subsequent client calls.
- Provide SoapHeader objects and avoid placing secret tokens into debug logs.
- __soapCall performs the actual SOAP operation.
Select this API or pattern based on its documented behavior and the trust boundaries in your own application, and test all external integrations against their actual deployments.