How to use soap class in php (with example)?

How to use soap class in php (with example)?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Decoding SOAP Requests in PHP: A Practical Look at GlobalWeather

SOAP (Simple Object Access Protocol) remains a rigid but structured standard for exchanging structured information in web services, often preferred in enterprise environments where strict contract enforcement is required. Unlike REST, which relies on flexible JSON payloads and HTTP verbs, SOAP mandates the use of XML envelopes and specific headers. This rigidity provides type safety and built-in error handling mechanisms like Fault objects, but it requires a developer to understand the underlying structure before writing code.

When interacting with an external service like the GlobalWeather API via its .asmx endpoint, the first step is understanding the request payload format. The protocol demands a specific XML envelope that wraps your actual data parameters. As seen in the raw HTTP exchange, the Content-Type header must be set to application/soap+xml, and the body must strictly follow the SOAP 1.2 envelope structure defined by the W3C.

<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
               xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
               xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <GetWeather xmlns="http://www.webserviceX.NET">
      <CityName>London</CityName>
      <CountryName>UK</CountryName>
    </GetWeather>
  </soap:Body>
</soap:Envelope>

In PHP, manually constructing this XML string is possible but error-prone. A more robust approach utilizes the SoapClient class, which handles the serialization of arguments into the correct envelope format automatically. To initiate a connection to the GlobalWeather service, you instantiate a new SoapClient object, passing the WSDL URL if available, or simply the endpoint URL for basic invocation.

$client = new SoapClient('http://www.webservicex.net/globalweather.asmx', [
    'location' => 'http://www.webserviceX.NET', // Namespace location often needed for .asmx endpoints
    'soap_version' => SOAP_1_2,                 // Matching the server's preference
    'trace' => true                             // Useful for debugging raw requests/responses
]);

// Calling the method with named arguments matching the XML structure
$result = $client->GetWeather(['CityName' => 'London', 'CountryName' => 'UK']);

The return value from such a call is typically a complex object containing the response data wrapped in properties that mirror the XML tags. In this specific example, the response contains a GetWeatherResult property. To extract the actual weather string, you must navigate through these nested objects: $result->GetWeatherResult.

A critical trade-off with SOAP is its verbosity. Every request includes the full XML envelope, even for simple data types. This increases payload size compared to JSON-based REST APIs and adds parsing overhead. Furthermore, debugging SOAP errors can be tricky because exceptions are often returned as SOAP Faults rather than standard PHP exceptions unless explicitly handled or configured via registerSoapFault.

When processing this data in a production environment, you must validate the response structure before accessing properties. Relying on loose typing is dangerous when consuming third-party APIs that may change their schema without immediate notice. Always check if the GetWeatherResult property exists and is not empty before attempting to parse the weather string.

if (isset($result->GetWeatherResult)) {
    echo $result->GetWeatherResult;
} else {
    // Handle potential SOAP Fault or null response
    throw new Exception('Failed to retrieve weather data');
}

While modern web development often favors RESTful services for their simplicity, understanding SOAP remains essential for maintaining legacy systems or integrating with enterprise applications that strictly adhere to WS-* standards. The SoapClient class abstracts much of the complexity, but the developer must still respect the protocol's constraints regarding namespaces and encoding styles.

Our network