2011-03-24 68 views
1

我正在構建一個使用php核心SOAP類的Web服務,並且需要返回一個枚舉類型的變量。這是在WSDL類型定義:如何在PHP SOAP服務器中返回枚舉類型

<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

回答

1

我解決了它。有關服務器沒有加載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> 
0

檢查出來:

Calling web service (SOAP) with PHP involving enums

枚舉只規定允許值。只要你傳回一個評估爲「OK」,「INTERNAL_ERROR」或「TOO_MANY_REQUESTS」的字符串,那麼它應該可以正常工作。

+0

謝謝,但它是不一樣的問題。由於我正在建立一個服務器,而不是客戶端,我必須返回正確的類型。 – 2011-03-24 23:01:15