How to Build PHP SOAP Web Services: Step‑by‑Step Guide with Code

This article explains how to use PHP's built‑in SOAP extension to create a client, call web service methods, handle complex parameters, add authentication headers, and manage errors, providing complete code examples for each step.

php Courses
php Courses
php Courses
How to Build PHP SOAP Web Services: Step‑by‑Step Guide with Code

Overview of SOAP

SOAP (Simple Object Access Protocol) is an XML‑based messaging protocol that uses HTTP as its transport. It defines a standard envelope for request and response messages, enabling interoperable communication between heterogeneous platforms and programming languages.

Using SOAP in PHP

PHP provides a built‑in SoapClient class. Before using it, verify that the SOAP extension is enabled (e.g., by calling phpinfo() and checking for the soap section).

Creating a client and invoking a method

<?php
$wsdl = "http://example.com/webservice?wsdl"; // URL of the WSDL document
$client = new SoapClient($wsdl); // instantiate the client

$param1 = "value1";
$param2 = "value2";

try {
    $response = $client->WebServiceMethod($param1, $param2);
    var_dump($response);
} catch (SoapFault $e) {
    // handle SOAP‑level errors
    echo "Error: " . $e->getMessage();
}
?>

The call is wrapped in a try‑catch block because the remote service may raise a SoapFault exception for protocol‑level errors, authentication failures, or invalid parameters.

Passing complex data structures

When a service expects a composite type, provide an associative array that matches the element names defined in the WSDL.

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

$person = [
    "name"    => "Zhang San",
    "age"     => 25,
    "address" => "Beijing"
];

try {
    $response = $client->WebServiceMethod($person);
    var_dump($response);
} catch (SoapFault $e) {
    echo "Error: " . $e->getMessage();
}
?>

Adding authentication with SOAP headers

If the service requires credentials, construct a custom XML fragment and attach it as a SoapHeader. The header is then set on the client with __setSoapHeaders().

<?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();
}
?>

In this example the XML fragment is escaped ( < and >) so that it remains literal code inside the PHP string.

Key points

Enable the PHP SOAP extension and confirm its availability.

Instantiate SoapClient with the service’s WSDL URL.

Pass simple parameters as scalar values; pass complex types as associative arrays that mirror the WSDL schema.

Wrap calls in try‑catch blocks to capture SoapFault exceptions.

When authentication is required, build a SOAP header (XML fragment) and attach it via __setSoapHeaders().

Following these steps allows reliable, cross‑platform data exchange between PHP applications and SOAP‑based web services.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendPHPAPIWeb servicesSOAP
php Courses
Written by

php Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.