Monthly Archives: May 2011

SOAP server with PHP

I hated SOAP. I really did and I do today but less. I’m developing web about 10 years and each time when I had to confront SOAP protocol I had nightmares while coding.

And to make things worst, one of my last projects was to build API server that will use WSDL-SOAP protocol in PHP. At beginning I explained to the guys why JSON is much better protocol, they even agreed with me but they service is already done, so it’s just our part to integrate it.

On my surprise, I had less problem writing SOAP server then it’s client. All what is need in PHP is few lines in code…

require_once 'class/soap_server.php';
try {
    $server = new SoapServer(null,array('uri'=>'http://confiq.org/soap_server/'));
    $server->setClass("soap_server");
    $server->handle();
} catch (Exception $e) {
    _log($e->getMessage() . ' in '.$e->getFile().'#'.$e->getLine());
    $server->fault($e->getCode(), $e->getMessage());
}

And there you go!
Of course, you’ll need to have soap_server object around with methods that will exist in soap server!

And for client:

$client = new SoapClient(null,array(
    "uri"            => "http://confiq.org/soap_server/",
    "location"        => "http://confiq.org/soap_server/",
    "trace"          => 1,
    "soap_version"     => SOAP_1_1,
    "exceptions"     => 1));
try {
    $demo = $client->someRandomMethod('email','email');
    fn_print_r($demo);
  } catch (SoapFault $exception) {
    fn_print_r($client->__getLastResponse());
    fn_print_die($exception);
}

And that’s it! :)

Worth reading about this topic, PHP SOAP Extension by Dmitry Stogov!