gpt4 book ai didi

PHP Soap 服务器响应格式

转载 作者:可可西里 更新时间:2023-11-01 13:30:31 26 4
gpt4 key购买 nike

我正在用 PHP 制作一个 SOAP 网络服务,它必须满足客户 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 网络服务的方式:

<?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 绑定(bind)样式。

像这样:

服务器.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();
}

客户端.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 回复。因此,如果客户端是有效的 soap 客户端,它应该能够解析响应并仍然使用它。

关于PHP Soap 服务器响应格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23537231/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com