SOAP and PHP in 2014

Posted by Unknown on Friday, January 31, 2014


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.


In PHP you can expose functions, classes or objects as server by invoking SOAPServer#addFunction , SOAPServer#setClass or SOAPServer#setObject() .




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.





<?php
// server.php
class MyService
{
public function add($x, $y)
{
return $x + $y;
}
}

$options = array(
'uri' => 'http://server/namespace',
'location' => 'http://server/location',
);

$server = new SOAPServer(null, $options);
$server->setObject(new MyService());
$server->handle();


The client is as simple as the following lines of code:





<?php
// client.php
$options = array(
'uri' => 'http://server/namespace',
'location'




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




more

{ 0 comments... » SOAP and PHP in 2014 read them below or add one }

Post a Comment

Popular Posts