SyntaxStudy
Sign Up
PHP SOAP Web Services in PHP
PHP Intermediate 5 min read

SOAP Web Services in PHP

PHP SOAP Client

PHP's built-in SoapClient class communicates with SOAP web services. It automatically handles XML encoding/decoding from the WSDL definition.

Example
<?php
// Connect to SOAP service using WSDL
$client = new SoapClient("https://api.example.com/service?wsdl", [
    "trace"      => true,
    "exceptions" => true,
]);

try {
    // Call a remote method
    $result = $client->getWeather(["city" => "London"]);
    echo $result->temperature;
} catch (SoapFault $e) {
    echo "SOAP Error: " . $e->faultstring;
}

// Debug
echo $client->__getLastRequest();
echo $client->__getLastResponse();
Pro Tip

Enable trace => true when debugging SOAP calls — __getLastRequest() and __getLastResponse() show the raw XML.