dongle7553 2011-09-27 07:08
浏览 56
已采纳

WSDL和SOAP:使用方法返回对象

Is there a way to return in soap an object with his methods? If i return xsd:struct in WSDL i only get the properties of the object but i can't use any of the methods.

For example

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

So after fetching the object:

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

Thanks.

  • 写回答

1条回答 默认 最新

  • dsagzmosl32217092 2011-09-27 08:12
    关注

    You cannot do this with SOAP. Basically your PHP class is being mapped to an XML data structure that's defined by an XML schema. This mapping only includes properties and cannot include executable code. SOAP is designed for interoperability and naturally you cannot share code between, let's say, PHP and Java or .NET. On the receiving side your XML data structure is being transformed into a data structure of the client's programming language (a PHP class if you use SoapClient or a C# class if you use C#). As the XML data structure only carries property information the executable part of the originating class cannot be rebuilt.

    But there is one thing that can help if both the SOAP server and the connecting client have access to the same code base (which means the same classes). You can define a mapping between a XML type and a PHP class in the SoapClient's constructor using the classmap-option. This allows SoapClient to map incoming XML data structures to real PHP classes - given the fact that both the server and the client have access to the relevant class definition. This allows you to use methods on the receiving side of the SOAP communication.

    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();
    

    The WSDL might look like (copied from one of the SoapClient tests and 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 might be interesting if you're doing SOAP in PHP.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)