如何在 PHP SOAP 服务器中返回枚举类型
我正在使用 php 核心 SOAP 类构建一个 Web 服务,并且需要返回一个枚举类型的变量。这是 WSDL 中的类型定义:
<xsd:simpleType name="ErrorCodeEnum">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="OK"/>
<xsd:enumeration value="INTERNAL_ERROR"/>
<xsd:enumeration value="TOO_MANY_REQUESTS"/>
</xsd:restriction>
</xsd:simpleType>
server.php:
<?php
class testclass {
public function testfunc($param) {
$resp = new testResp();
$resp->errorCode = 'OK'; #SoapServer returns xsd:string type.
return $resp;
}
}
class testReq {}
class testResp {
public $errorCode;
}
$class_map = array('testReq' => 'testReq', 'testResp' => 'testResp');
$server = new SoapServer (null, array('uri' => 'http://test-uri/', 'classmap' => $class_map));
$server->setClass ("testclass");
$server->handle();
?>
答案:
<ns1:testResponse>
<return xsi:type="SOAP-ENC:Struct">
<errorCode xsi:type="xsd:string">OK</errorCode>
</return>
</ns1:testResponse>
如何返回类型 ErrorCodeEnum
而不是 string
?
I am building a web service using php core SOAP classes, and need to return a variable of enumerated type. This is the type definition in WSDL:
<xsd:simpleType name="ErrorCodeEnum">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="OK"/>
<xsd:enumeration value="INTERNAL_ERROR"/>
<xsd:enumeration value="TOO_MANY_REQUESTS"/>
</xsd:restriction>
</xsd:simpleType>
server.php:
<?php
class testclass {
public function testfunc($param) {
$resp = new testResp();
$resp->errorCode = 'OK'; #SoapServer returns xsd:string type.
return $resp;
}
}
class testReq {}
class testResp {
public $errorCode;
}
$class_map = array('testReq' => 'testReq', 'testResp' => 'testResp');
$server = new SoapServer (null, array('uri' => 'http://test-uri/', 'classmap' => $class_map));
$server->setClass ("testclass");
$server->handle();
?>
Answer:
<ns1:testResponse>
<return xsi:type="SOAP-ENC:Struct">
<errorCode xsi:type="xsd:string">OK</errorCode>
</return>
</ns1:testResponse>
How can i do to return type ErrorCodeEnum
instead of string
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看看:
使用涉及枚举的 PHP 调用 Web 服务 (SOAP)
枚举仅指定允许的值。只要您传回一个计算结果为“OK”、“INTERNAL_ERROR”或“TOO_MANY_REQUESTS”的字符串,那么它应该可以正常工作。
Check it out:
Calling web service (SOAP) with PHP involving enums
Enumeration only specifies allowable values. As long as you're passing back a string which evaluates as "OK", "INTERNAL_ERROR", or "TOO_MANY_REQUESTS", then it should work fine.
我解决了。服务器未加载 WSDL 文件存在一些问题。这是 WSDL 中的
types
部分:实际服务器答案:
I solved it. There was some problem about server not loading the WSDL file. This is the
types
section in the WSDL:Actual server answer: