2014-01-31

SOAP and PHP in 2014

“The SOAP stack is generally regarded as an embarrassing failure these days.”
– Tim Bray

While the quote by Tim Bray is still true to some degree, the toolstack and
possiblities behind SOAP are still superior to REST in my opinion. REST still
requires alot of manual coding, where RPC with SOAP allows much automation with
tools.

These last years REST has gotten all the buzz, everybody seems to be using it
and there are lots of talks about REST on conferences. SOAP used to be big for
building APIs, but everybody seems to hate it for various reasons. I have used
SOAP in several projects over the last 10 years and this blog post is a random
collection of information about the state of SOAP in PHP 2014, as a reminder
to myself and to others as well.

Why care about SOAP in 2014? For server to server (RPC) communication it still
has massive time to market and stability benefits over REST. The REST toolchain
is just not well developed enough, it lacks:

a standard to describe the input/output formats of endpoints

a way to “just do it”

ways to automatically generate clients in any language

While solutions exist for these problems in the REST space, they are often not
standardized and don’t serve the full stack and different languages.

WSDLs for SOAP however allow you to generate servers and clients from
datastructures by the click of a button or execution of a script. I can get two
servers communicating over SOAP, exposing all service methods in literally
minutes.

Basics

SOAP is a protocol for Remote-Procedure Calls. HTTP is used as mechanism
to talk between client and servers by sending POST requests with XML request
and response bodies.

The SOAPServer and SOAPClient objects ship with PHP core by default.

You can expose functions, classes or objects as server by registering
service callbacks with
SOAPServer#addFunction, SOAPServer#setClass or
SOAPServer#setObject and then calling the handle() method, which
handles request and response generation and sending in one step. You can
normally exit the request directly after handle.

The client is even simpler, it overwrites the __call magic method.
Any call to the client therefore looks exactly like the same call
on the server in your code.

PHPs Non-WSDL mode

One argument against SOAP is the requirement to define WSDL documents for
both server and client to allow communication. This is not true for Clients and
Servers both written in PHP. You can use the non-WSDL mode to expose an object
from the server and use a non-wsdl client to talk to it. The PHP SOAPClient
and SOAPServer have a common exchange format that is used in this case
and replaces WSDL entirely.

Truncated by Planet PHP, read more at the original (another 22395 bytes)

Show more