【发布时间】:2014-06-25 13:51:26
【问题描述】:
我正在用 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 网络服务的方式:
<?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 响应?
【问题讨论】:
-
我在这里看到两个问题:a) 您错过了将 XSD 文件添加到您的问题 - 但您询问了它。 b) 您错过了根据 XSD 要求说出您在哪里看到的差异。两个 XML 块看起来都具有相同的元素(只是缺少一个空标头,我认为在 SOAP 中可以忽略它)。所以从格式化的角度来看,没有什么可做的,它不清楚你要的是什么。
-
我编辑了问题,添加了 XSD 文件
-
答案中缺少
CheckversionResult元素,看起来它被替换为<return xsi:type="enc:Struct">,我想知道这是怎么回事。是 WSDL 模式吗? -
不,这是非 WSDL 模式。我还从客户那里收到了一个 WSDL,说他们是 XSD 和 WSDL 文件的主人,所以我不应该自己生成这些文件。然而,这让我很困惑。 WSDL 文件不是将 SOAP 客户端指向 SOAP 服务器的文件吗?那么他们的 WSDL 文件应该如何指向我的 SOAP 服务器呢?
-
WSDL 文件定义了服务。它可以由 SOAP 客户端使用,以便知道远程过程(以及参数和返回值的类型),还可以为服务器定义相同的过程。 XSD 定义了类型。两者都可以用于客户端和服务器。通常使用 WSDL 更容易实现。
标签: php xml web-services soap xsd