PHP5 Web Service(SOAP)標準範例

PHP5 Web Service(SOAP)標準範例

PHP5 Web Service(SOAP)標準範例

 

資料來源: https://www.youtube.com/watch?v=UEpdhxM1ikk

 

test.wsdl

<?xml version=”1.0″?>

<wsdl:definitions name=’MyDefinition’

  targetNamespace=’urn:myTns’

  xmlns:tns=’urn:myTns’

  xmlns:wsdl=’http://schemas.xmlsoap.org/wsdl/’

  xmlns:xsd=’http://www.w3.org/2001/XMLSchema’ 

  xmlns:soap12=’http://schemas.xmlsoap.org/wsdl/soap12/’

  xmlns=’http://schemas.xmlsoap.org/wsdl/’>

 

        <wsdl:message name=’SumRequest’>

                <wsdl:part name=’Sum_a’ type=’xsd:int’/>

                <wsdl:part name=’Sum_b’ type=’xsd:int’/>

        </wsdl:message>

        <wsdl:message name=’SumResponse’>

                <wsdl:part name=’Sum_Result’ type=’xsd:string’/>

        </wsdl:message>

 

        <wsdl:message name=’SubRequest’>

                <wsdl:part name=’Sub_a’ type=’xsd:int’/>

                <wsdl:part name=’Sub_b’ type=’xsd:int’/>

        </wsdl:message>

        <wsdl:message name=’SubResponse’>

                <wsdl:part name=’Sub_Result’ type=’xsd:string’/>

        </wsdl:message>

 

        <wsdl:portType name=’MyPortType’>

         

                <wsdl:operation name=’SumData’>

                        <input message=’tns:SumRequest’/>

                        <output message=’tns:SumResponse’/>

                </wsdl:operation>

         

                <wsdl:operation name=’SubData’>

                        <input message=’tns:SubRequest’/>

                        <output message=’tns:SubResponse’/>

                </wsdl:operation>

         

        </wsdl:portType>

 

        <wsdl:binding name=’MyBinding’ type=’tns:MyPortType’>

         

                <soap12:binding style=’document’ transport=’http://schemas.xmlsoap.org/soap/http’/>

 

                <wsdl:operation name=’SumData’>

                        <soap12:operation soapAction=”/>

                        <wsdl:input>

                                <soap12:body use=’literal’ namespace=’urn:myInputNamespaces’ encodingStyle=’http://schemas.xmlsoap.org/soap/encoding/’/>

                        </wsdl:input>

                        <wsdl:output>

                                <soap12:body use=’literal’ namespace=’urn:myOutputNamespaces’ encodingStyle=’http://schemas.xmlsoap.org/soap/encoding/’/>

                        </wsdl:output>

                </wsdl:operation>

         

                <wsdl:operation name=’SubData’>

                        <soap12:operation soapAction=”/>

                        <wsdl:input>

                                <soap12:body use=’literal’ namespace=’urn:myInputNamespaces’ encodingStyle=’http://schemas.xmlsoap.org/soap/encoding/’/>

                        </wsdl:input>

                        <wsdl:output>

                                <soap12:body use=’literal’ namespace=’urn:myOutputNamespaces’ encodingStyle=’http://schemas.xmlsoap.org/soap/encoding/’/>

                        </wsdl:output>

                </wsdl:operation> 

         

        </wsdl:binding>

 

        <wsdl:service name=’MyService’>

                <wsdl:port name=’MyPort’ binding=’MyBinding’>

                        <soap12:address location=’HTTP的絕對路徑/testserver.php’/>

                </wsdl:port>

        </wsdl:service>

 

</wsdl:definitions>

testclient.php

<?php

    $client = new SoapClient(“HTTP的絕對路徑/test.wsdl”, array(‘soap_version’ => SOAP_1_2,’trace’ =>  1 ));

        $a=200;

        $b=100;

    $return = $client->__soapCall(“SumData”,array(‘Sum_a’ => $a,’Sum_b’ =>$b));

    print_r($return);

        echo ‘<br>’;

        $return1 = $client->__soapCall(“SubData”,array(‘Sub_a’ => $a,’Sub_b’ =>$b));

        print_r($return1);

?>

testserver.php

<?php

class TestClass

{

        function SumData($a,$b)

        {

                return $a+$b;

        }

        function SubData($a,$b)

        {

                return $a-$b;

        }      

}

ini_set(“soap.wsdl_cache_enabled”, “0”); // disabling WSDL cache

$server = new SoapServer(‘HTTP的絕對路徑/test.wsdl’, array(‘soap_version’   => SOAP_1_2));

$server->setClass(‘TestClass’);

$server->handle();

?>

 

 

 

發表迴響

你的電子郵件位址並不會被公開。 必要欄位標記為 *