How to Build PHP SOAP Web Services: Step‑by‑Step Guide with Code
This guide explains how to use PHP’s built‑in SOAP extension to create a SOAP client, call web‑service methods with simple or complex parameters, handle responses and exceptions, and add authentication headers, providing complete code examples for each step.
SOAP Protocol Overview
SOAP (Simple Object Access Protocol) is an XML‑based messaging protocol that runs over HTTP. It defines a strict envelope structure ( <Envelope>, <Header>, <Body>) so that heterogeneous platforms can exchange typed data.
Using PHP’s built‑in SOAP extension
Before writing code, confirm that the soap extension is enabled (e.g., run phpinfo() and look for the SOAP section). The extension provides two main classes: SoapClient – for invoking operations defined in a WSDL. SoapServer – for exposing a service (not covered here).
Typical client code follows these steps:
Instantiate SoapClient with the WSDL URL and optional configuration array (e.g., ['trace'=>true, 'exceptions'=>true] to aid debugging).
Prepare the parameters – they can be simple scalars or associative arrays that map to complex types defined in the WSDL.
Call the desired operation as a method on the client object.
Wrap the call in a try…catch block to handle SoapFault exceptions.
<?php
$wsdl = "http://example.com/webservice?wsdl";
$options = [
"trace" => true,
"exceptions" => true
];
$client = new SoapClient($wsdl, $options);
$param1 = "value1";
$param2 = "value2";
try {
$response = $client->WebServiceMethod($param1, $param2);
var_dump($response);
} catch (SoapFault $e) {
echo "Error: " . $e->getMessage();
}
?>Passing complex parameters
When the WSDL defines a complex type, pass an associative array whose keys match the element names. PHP automatically serialises the array into the required XML structure.
<?php
$wsdl = "http://example.com/webservice?wsdl";
$client = new SoapClient($wsdl);
$person = [
"name" => "张三",
"age" => 25,
"address" => "北京市"
];
try {
$response = $client->WebServiceMethod($person);
var_dump($response);
} catch (SoapFault $e) {
echo "Error: " . $e->getMessage();
}
?>Adding SOAP authentication headers
Many services require a custom header (e.g., username/password). Build the XML fragment, wrap it in a SoapHeader object, and attach it with __setSoapHeaders() before invoking any operation.
<?php
$wsdl = "http://example.com/webservice?wsdl";
$client = new SoapClient($wsdl);
$username = "myUser";
$password = "myPass";
$authXml = "<auth><username>{$username}</username><password>{$password}</password></auth>";
$header = new SoapHeader("http://example.com", "Authentication", $authXml);
$client->__setSoapHeaders($header);
try {
$response = $client->WebServiceMethod();
var_dump($response);
} catch (SoapFault $e) {
echo "Error: " . $e->getMessage();
}
?>Best practices and common pitfalls
Always enable exceptions or check $client->__getLastResponse() when debugging.
Use the trace option to retrieve the raw request/response XML for troubleshooting.
When passing complex types, ensure the array keys exactly match the element names defined in the WSDL; otherwise the service may reject the request.
Secure credentials: avoid hard‑coding usernames/passwords in source files; consider loading them from environment variables or a protected configuration store.
Set appropriate timeout values (e.g., default_socket_timeout) for long‑running operations.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
php Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
