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 func开发者_运维技巧tion 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
?
I solved it. There was some problem about server not loading the WSDL file. This is the types
section in the WSDL:
<wsdl:types>
<xsd:schema targetNamespace="http://schema.example.com">
<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>
<xsd:complexType name="testResp">
<xsd:all>
<xsd:element name="errorCode" type="xsd:ErrorCodeEnum"/>
</xsd:all>
</xsd:complexType>
</xsd:schema>
</wsdl:types>
Actual server answer:
<SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://schema.example.com" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<ns1:testResponse>
<testReturn xsi:type="ns1:testResp">
<errorCode xsi:type="xsd:ErrorCodeEnum">OK</errorCode>
</testReturn>
</ns1:testResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
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.
精彩评论