簡體   English   中英

PHP Soap Server響應格式

[英]PHP Soap Server response formatting

我正在用PHP創建一個SOAP Web服務,它必須符合客戶端XSD文件的要求。

以下是客戶提供的XSD文件的鏈接: http//pastebin.com/MX1BZUXc

他們期待的回應如下:

[根據理論認為問題不是與空白相關的,有些長線可讀性很差。]

<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>

但是,我目前得到的回應如下:

<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>

這就是我創建SOAP Web服務的方式:

<?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();
    }

}

這就是我打電話給我的網絡服務的方式:

$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();

有誰知道我如何根據我提供的XSD文件正確格式化我的SOAP響應?

您必須構造一個正確的wsdl文件。 現在您的服務器以默認的rpc樣式運行。 嘗試使用: http//framework.zend.com/manual/1.12/en/zend.soap.autodiscovery.html,使用不同的WSDL綁定樣式。

像這樣的東西:

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();

有了這個我有這個:

<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>

旁注:我認為您的回復是有效的肥皂回復。 因此,如果客戶端是有效的SOAP客戶端,它應該能夠解析響應並仍然使用它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM