如何在PHP SOAP服务器中返回枚举类型

时间:2011-03-24 21:39:11

标签: php soap wsdl

我正在使用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

2 个答案:

答案 0 :(得分:2)

我解决了。服务器未加载WSDL文件存在一些问题。这是WSDL中的types部分:

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

实际服务器答案:

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

答案 1 :(得分:0)

检查出来:

Calling web service (SOAP) with PHP involving enums

枚举仅指定允许的值。只要你传回一个评估为“OK”,“INTERNAL_ERROR”或“TOO_MANY_REQUESTS”的字符串,那么它应该可以正常工作。