Session persistent SOAP Server

Wie man eine PHP-Frontend-Session an einen SOAP-Server im Backend weiterreichen kann.

26.09.2016 - Manuel Wohlers

Zugegeben, in Zeiten zustandsloser REST-Services mag es etwas merkwürdig anmuten.

Für ein Projekt musste allerdings eine Frontend-Session an den SOAP-Server im Backend "weitergereicht" werden.

Wie das funktioniert, sei hier anhand dieses Beispiels festgehalten:

Der Client

<?php /** * The Client part: */  // start local session (if not startet yet)... session_start();  // create the client... // ini_set("soap.wsdl_cache_enabled", 0); $options = array(  // 'location' => 'http://api-location/',  // 'trace' => true, ); $wsdl = 'http://api-location/?wsdl'; $client = new SoapClient($wsdl, $options);  // re-use remote session if session id known (set cookies): if (isset($_SESSION['soap_cookies'])) {  foreach ($_SESSION['soap_cookies'] as $cookieName =>$cookieValues) {    $client->__setCookie($cookieName, $cookieValues[0]);  } } else {  // create session by calling some methods...  // The first request will create the session and though fill $client->_cookies  // with the required session id information. It is not required to write a  // particular "hello" function for this purpose – it should be something useful ;-)  $client->hello();  $_SESSION['soap_cookies'] = $client->_cookies; }  // test call: $testValue = $client->getTestValue();  if (!$testValue) {  $testValue = rand(1,10);  $client->setTestValue($testValue); }  $result = $client->getTestValue();  if ($testValue == $result) {  echo 'SoapAPI is session persistent.'; } else {  echo 'SoapAPI is NOT session persistent.'; }  

Der Server

<?php /** * The Server part: */  // starting server session... session_start();  // create the soap server...   $options=array(  'uri'=>'http://abc.def/my_uri',  //'soap_version' => SOAP_1_2,  //'style' => SOAP_DOCUMENT, ); $server = new SoapServer(null, $options); $server->setClass('\My\Server\Controller');  // setPersistence has to be called after setClass because setClass would // set persistance option back to SESSION_PERSISTENCE_REQUEST (non-persistent) $server->setPersistence(SOAP_PERSISTENCE_SESSION);  // at last handle the request: return $server->handle();