2011-09-27 24 views
3

有沒有方法可以用他的方法在soap中返回一個對象?如果我返回WSDL中的xsd:struct,我只獲取對象的屬性,但我不能使用任何方法。WSDL和SOAP:使用方法返回對象

例如

class person 
{ 
    var $name = "My name"; 
    public function getName() 
    { 
     return $this->name; 
    } 
} 

獲取對象之後所以:

$client = new SoapClient(); 
$person = $client->getPerson(); 
echo $person->getName(); // Return "My Name"; 

感謝。

回答

3

你不能這樣做SOAP。基本上,您的PHP類正在映射到由XML模式定義的XML數據結構。該映射只包含屬性,不能包含可執行代碼。 SOAP是專爲互操作性而設計的,當然,您不能在PHP和Java或.NET之間共享代碼。在接收端,您的XML數據結構正在轉換爲客戶端編程語言的數據結構(如果您使用SoapClientC#類,如果使用C#,則爲PHP類)。由於XML數據結構僅包含屬性信息,因此不能重建始發類的可執行部分。

但是如果SOAP服務器和連接客戶端都可以訪問相同的代碼庫(這意味着相同的類),那麼有一件事可以提供幫助。您可以使用classmap選項在SoapClient的構造函數中定義XML類型和PHP類之間的映射。這允許SoapClient將傳入的XML數據結構映射到真正的PHP類 - 考慮到服務器和客戶端都可以訪問相關的類定義。這使您可以使用SOAP通信接收端的方法。

class book { 
    public $a = "a"; 
    public $b = "c"; 
    public function getName() { 
     return $this->a.' '.$this->b; 
    } 
} 

$options = array(
    'classmap' => array('book' => 'book') 
); 
$client = new SoapClient('path/to/wsdl', $options); 
$book = $client->test(); 
echo $book->getName(); 

WSDL可能看起來像(從SoapClient測試的一個複製和adpated):

<wsdl:definitions xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://schemas.nothing.com" targetNamespace="http://schemas.nothing.com"> 
    <wsdl:types> 
     <xsd:schema targetNamespace="http://schemas.nothing.com"> 
      <xsd:complexType name="book"> 
       <xsd:all> 
        <xsd:element name="a" type="xsd:string"/> 
        <xsd:element name="b" type="xsd:string"/> 
       </xsd:all> 
      </xsd:complexType> 
    </xsd:schema> 
    </wsdl:types> 
    <message name="testRequest"> 
    </message> 
    <message name="testResponse"> 
     <part name="res" type="tns:book"/> 
    </message> 
    <portType name="testPortType"> 
     <operation name="test"> 
      <input message="tns:testRequest"/> 
      <output message="tns:testResponse"/> 
     </operation> 
    </portType> 
    <binding name="testBinding" type="tns:testPortType"> 
     <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/> 
     <operation name="test"> 
      <soap:operation soapAction="http://localhost:81/test/interface.php?class=test/dotest" style="rpc"/> 
      <input> 
       <soap:body use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://schemas.nothing.com"/> 
      </input> 
      <output> 
       <soap:body use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://schemas.nothing.com"/> 
      </output> 
     </operation> 
    </binding> 
    <service name="test"> 
     <port name="testPort" binding="tns:testBinding"> 
      <soap:address location="http://localhost:81/test/interface.php?class=test"/> 
     </port> 
    </service> 
</wsdl:definitions> 

The State of SOAP in PHP,如果你在PHP做SOAP可能是有趣的。

+0

噢謝謝你。你的溶劑很好,但不符合我的需求。我需要將這些方法隱藏起來。謝謝 – keepwalking

+0

你想在客戶端使用方法,但想隱藏它們嗎?我不認爲這是行不通的... –

+0

如果我在SoapServer中使用setClass,我可以使用隱藏方法:)無論如何,我可以通過返回一個客戶端soap對象來做一個解決方法。 – keepwalking