Skip to main content

SOAP Web Services Introduction

SOAP (Simple Object Access Protocol) is a messaging protocol for exchanging structured information in web services. In this course, you’ll learn to create and consume SOAP services using PHP.

What is SOAP?

SOAP is an XML-based protocol that allows programs running on different operating systems to communicate with each other. It defines:
  • Message format: XML structure for requests and responses
  • Communication protocol: Typically HTTP/HTTPS
  • Service description: WSDL (Web Services Description Language)
SOAP is platform-independent and language-independent, making it ideal for enterprise integrations.

SOAP vs REST

FeatureSOAPREST
ProtocolStrict protocol with rulesArchitectural style
FormatXML onlyJSON, XML, others
TransportHTTP, SMTP, TCPPrimarily HTTP
StateCan be stateful or statelessStateless
ComplexityMore complexSimpler
SecurityWS-Security standardHTTPS, OAuth

SOAP Message Structure

A SOAP message is an XML document with three main parts:
<?xml version="1.0"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope">
  <soap:Header>
    <!-- Optional header information -->
  </soap:Header>
  <soap:Body>
    <!-- Required body with actual message -->
    <m:GetPvp xmlns:m="http://www.example.com/product">
      <m:codP>13</m:codP>
    </m:GetPvp>
  </soap:Body>
</soap:Envelope>

Message Components

1

Envelope

The root element that identifies the XML document as a SOAP message. Required in every SOAP message.
2

Header (Optional)

Contains application-specific information like authentication credentials, transaction IDs, or routing data.
3

Body (Required)

Contains the actual message intended for the recipient - either a request or a response.
4

Fault (Optional)

Used within the Body to indicate error messages and status information.

SOAP Request Example

When calling a SOAP service, your request might look like:
<?xml version="1.0"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope">
  <soap:Body>
    <m:getPvp xmlns:m="http://www.example.com/operaciones">
      <codP>13</codP>
    </m:getPvp>
  </soap:Body>
</soap:Envelope>

SOAP Response Example

The server responds with:
<?xml version="1.0"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope">
  <soap:Body>
    <m:getPvpResponse xmlns:m="http://www.example.com/operaciones">
      <return>29.99</return>
    </m:getPvpResponse>
  </soap:Body>
</soap:Envelope>

SOAP Fault Structure

When errors occur, SOAP uses a standardized fault structure:
<?xml version="1.0"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope">
  <soap:Body>
    <soap:Fault>
      <faultcode>soap:Client</faultcode>
      <faultstring>Invalid product code</faultstring>
      <detail>
        <error>Product with code 999 not found</error>
      </detail>
    </soap:Fault>
  </soap:Body>
</soap:Envelope>

PHP SOAP Extension

PHP provides built-in SOAP support through the SoapClient and SoapServer classes:
<?php
// Check if SOAP extension is loaded
if (!extension_loaded('soap')) {
    die('SOAP extension not available');
}

// SOAP is available - proceed with implementation
echo "SOAP extension is loaded and ready";
Ensure the PHP SOAP extension is enabled in your php.ini file:
extension=soap

When to Use SOAP

SOAP is ideal for:
  • Enterprise applications with strict contracts
  • Financial services requiring high security (WS-Security)
  • Legacy system integration where SOAP is already established
  • Complex operations requiring stateful interactions
  • Formal service contracts with WSDL documentation

Course Context

In TEMA-06, you’ll work with:
  • Creating SOAP servers with PHP classes
  • Generating WSDL files using php2wsdl
  • Building SOAP clients to consume services
  • Handling complex data types and arrays
  • Error handling and debugging SOAP messages

Next Steps

Now that you understand SOAP basics, proceed to:

WSDL Generation

Learn to generate WSDL from PHP classes

Consuming Services

Build SOAP clients to call web services

Additional Resources