Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Soap Server response formatting

I'm making a SOAP web service in PHP that has to fit the requirements of a client's XSD file.

Here is a link to the XSD file, supplied by the client: http://pastebin.com/MX1BZUXc

The response they are expecting looks like this:

[Some long lines broken for legibility, on the theory that the problem is not whitespace-related.]

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
  <s:Header />
  <s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
          xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <CheckVersionResponse xmlns="http://www.---.---/---">
      <CheckversionResult>
        <ValidationOk>true</ValidationOk>
        <VersionNumber>1.4.0</VersionNumber>
        <CurrentRemoteServerTime
          >2014-05-02T09:35:20.368+02:00</CurrentRemoteServerTime>
      </CheckversionResult>
    </CheckVersionResponse>
  </s:Body>
</s:Envelope>

However, the response I'm currently getting looks like this:

<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope" 
              xmlns:ns1="http://---.---/" 
              xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
              xmlns:enc="http://www.w3.org/2003/05/soap-encoding">
<env:Body xmlns:rpc="http://www.w3.org/2003/05/soap-rpc">
    <ns1:CheckVersionResponse 
      env:encodingStyle="http://www.w3.org/2003/05/soap-encoding">
        <rpc:result>return</rpc:result>
        <return xsi:type="enc:Struct">
            <ValidationOk xsi:type="xsd:int">1</ValidationOk>
            <VersionNumber xsi:type="xsd:string"
              >1.4.0</VersionNumber>
            <CurrentRemoteServerTime xsi:type="xsd:string"
              >2014-05-08T10:31:49</CurrentRemoteServerTime>
        </return>
    </ns1:CheckVersionResponse>
</env:Body>
</env:Envelope>

This is how I made my SOAP webservice:

<?php

/* Helper class for my response object */
class CheckVersionResult extends stdClass
{
    /** @var bool */
    public $ValidationOk = '';
    /** @var string */
    public $VersionNumber = '';
    /** @var string */
    public $CurrentRemoteServerTime = '';
}

/* SOAP interface class */
class MySoapClass
{
    /**
     * Returns version
     *
     * @param string $param1
     * @param string $param2
     * @return CheckVersionResult
     */
    public function CheckVersion($param1, $param2)
    {
        $data = new CheckVersionResult();
        $data->ValidationOk = 1;
        $data->VersionNumber = '1.4.0';
        $data->CurrentRemoteServerTime = date('Y-m-d\TH:i:s');
    }
}

/* Controller class */
class WebserviceController {

    public function indexAction() {
        $soap = new Zend_Soap_Server();
        $soap->setClass('MySoapClass');
        $soap->setUri("http://---.---/");
        $mySoapClass = new MySoapClass();
        $soap->setObject($mySoapClass);
        $soap->setSoapVersion(SOAP_1_2);
        $soap->handle();
    }

}

And this is how I call my webservice:

$client = new SoapClient(null, array(
    "soap_version" => SOAP_1_2,
    "location" => "http://---.---/webservice/index",
    "uri" => "http://---.---/",
    "trace" => 1, // enable trace to view what is happening
    "exceptions" => 0, // disable exceptions
    "cache_wsdl" => 0)   // no wsdl
);

$client->CheckVersion('param1', 'param2');
header('Content-Type: application/xml; charset=utf-8');
echo $client->__getLastResponse();
die();

Does anyone know how I can properly format my SOAP response according to the XSD file I got delivered?

like image 646
user1026090 Avatar asked May 08 '14 09:05

user1026090


1 Answers

You must construct a proper wsdl file. Right now your server is operating in default rpc style. Try using: http://framework.zend.com/manual/1.12/en/zend.soap.autodiscovery.html with different WSDL Binding Styles.

Something like this:

server.php:

<?php

error_reporting(E_ALL);
ini_set('display_errors', 1);

set_include_path(get_include_path().';./library/;./library/Zend');

require_once 'Zend/Loader/Autoloader.php';

$autoloader = Zend_Loader_Autoloader::getInstance();
$autoloader->setFallbackAutoloader(true);

/* Helper class for my response object */
class CheckVersionResult extends stdClass
{
    /** @var bool */
    public $ValidationOk = '';
    /** @var string */
    public $VersionNumber = '';
    /** @var string */
    public $CurrentRemoteServerTime = '';
}

/* SOAP interface class */
class MySoapClass
{
    /**
     * Returns version
     *
     * @param string $param1
     * @param string $param2
     * @return CheckVersionResult
     */
    public function CheckVersion($param1, $param2)
    {
        $data = new CheckVersionResult();
        $data->ValidationOk = 1;
        $data->VersionNumber = '1.4.0';
        $data->CurrentRemoteServerTime = date('Y-m-d\TH:i:s');

        return $data;
    }
}

$mySoapClass = new MySoapClass();


if(isset($_GET['wsdl'])) {
    $autodiscover = new Zend_Soap_AutoDiscover();
    $autodiscover->setClass('MySoapClass');

    $autodiscover->setOperationBodyStyle(
    array('use' => 'literal',
          'namespace' => 'http://localhost/stack/23537231/server.php')
    );

    $autodiscover->setBindingStyle(
        array('style' => 'document',
          'transport' => 'http://localhost/stack/23537231/server.php')
    );

    $autodiscover->handle();
} else {
    // pointing to the current file here
    $soap = new Zend_Soap_Server("http://localhost/stack/23537231/server.php?wsdl", array(
        'cache_wsdl'=> WSDL_CACHE_NONE,
        'classmap'  => array(
            'CheckVersionResult'=>'CheckVersionResult',
        )

    ));
    $soap->setClass('MySoapClass');
    $soap->handle();
}

client.php

<?php

error_reporting(E_ALL);
ini_set('display_errors', 1);

set_include_path(get_include_path().';./library/;./library/Zend');

require_once 'Zend/Loader/Autoloader.php';

$autoloader = Zend_Loader_Autoloader::getInstance();
$autoloader->setFallbackAutoloader(true);

/* Helper class for my response object */
class CheckVersionResult extends stdClass
{
    /** @var bool */
    public $ValidationOk = '';
    /** @var string */
    public $VersionNumber = '';
    /** @var string */
    public $CurrentRemoteServerTime = '';
}

$client = new SoapClient('http://localhost/stack/23537231/server.php?wsdl', array(
        "trace" => 1, // enable trace to view what is happening
        "exceptions" => 1, // disable exceptions
        "cache_wsdl" => WSDL_CACHE_NONE,
        'classmap'  => array(
            'CheckVersionResult'=>'CheckVersionResult',
        ))   // no wsdl
);

$ret = $client->CheckVersion('param1', 'param2');
header('Content-Type: application/xml; charset=utf-8');

echo $client->__getLastResponse();
die();

With this I have this:

<SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
  <SOAP-ENV:Body>
    <ns1:CheckVersionResponse>
      <return xsi:type="ns1:CheckVersionResult">
        <ValidationOk xsi:type="xsd:boolean">true</ValidationOk>
        <VersionNumber xsi:type="xsd:string">1.4.0</VersionNumber>
        <CurrentRemoteServerTime xsi:type="xsd:string">2014-05-19T22:22:59</CurrentRemoteServerTime>
      </return>
    </ns1:CheckVersionResponse>
  </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

Side note: I think that your response is a valid soap response. So if a client is a valid soap client it should be able to parse response and still use it.

like image 77
Greg Avatar answered Oct 14 '22 07:10

Greg