1。先生成WSDL的文檔,網站建設見代碼php
- <?xml version="1.0" encoding="utf-8"?>
- <wsdl:definitions
- xmlns:impl='http://wso2.org/wsf/php/helloService'
- xmlns:intf='http://wso2.org/wsf/php/helloService'
- xmlns:wsdl='http://schemas.xmlsoap.org/wsdl/'
- xmlns:wsdlsoap='http://schemas.xmlsoap.org/wsdl/soap/'
- xmlns:xsd='http://www.w3.org/2001/XMLSchema'
- targetNamespace='http://wso2.org/wsf/php/helloService'>
- <wsdl:types>
- <schema elementFormDefault='qualified'
- xmlns:impl='http://wso2.org/wsf/php/helloService'
- xmlns:intf='http://wso2.org/wsf/php/helloService'
- xmlns:wsdl='http://schemas.xmlsoap.org/wsdl/'
- xmlns="http://www.w3.org/2001/XMLSchema"
- targetNamespace='http://wso2.org/wsf/php/helloService' >
- <element name='greet'>
- <complexType>
- <sequence>
- <element name='name' type='xsd:string' />
- </sequence>
- </complexType>
- </element>
- <element name='greetResponse'>
- <complexType>
- <sequence>
- <element name='greetReturn' type='xsd:string' />
- </sequence>
- </complexType>
- </element>
- </schema>
- </wsdl:types>
- <wsdl:message name='greetRequest'>
- <wsdl:part name='parameters' element='impl:greet' />
- </wsdl:message>
- <wsdl:message name='greetResponse'>
- <wsdl:part name='parameters' element='impl:greetResponse' />
- </wsdl:message>
- <wsdl:portType name='helloService'>
- <wsdl:operation name='greet'>
- <wsdl:input name='greetRequest' message='impl:greetRequest' />
- <wsdl:output name='greetResponse' message='impl:greetResponse' />
- </wsdl:operation>
- </wsdl:portType>
- <wsdl:binding name='helloServiceSoapBinding' type='impl:helloService'>
- <wsdlsoap:binding transport='http://schemas.xmlsoap.org/soap/http' style='document' />
- <wsdl:operation name='greet'>
- <wsdlsoap:operation soapAction='helloService#greet' />
- <wsdl:input name='greetRequest'>
- <wsdlsoap:body use='literal' />
- </wsdl:input>
- <wsdl:output name='greetResponse'>
- <wsdlsoap:body use='literal' />
- </wsdl:output>
- </wsdl:operation>
- </wsdl:binding>
- <wsdl:service name='helloService'>
- <wsdl:port binding='impl:helloServiceSoapBinding' name='helloService'>
- <wsdlsoap:address location='http://10.168.2.5/ws/test1.php' /><!--要修改爲本身的URL地址-->
- </wsdl:port>
- </wsdl:service>
- </wsdl:definitions>
2.生成Web Service服務端,提供Web Service調用。網站
- <?php
- function greet($param) {
- $retval = 'Hello '.$param->name;
- $result = array('greetReturn' => $retval);
- return $result;
- }
- ini_set("soap.wsdl_cache_enabled", "0"); //低版本的PHP一個Bug,必須加這個。高於5.23版本能夠不用加
- $server = new SoapServer('hello.wsdl',array('soap_version' => SOAP_1_2));
- $server->addFunction('greet');
- $server->handle();
- ?>
3.客戶端調用這個Web Service,見代碼:spa
- <?php
- try {
- $client = new SoapClient('http://10.168.2.5/ws/test1.php?wsdl'); //修改爲本身的URL地址
- $result = $client->__soapCall('greet', array(array('name' => 'PHP Web Service')));
- printf("Result = %s", $result->greetReturn);
- } catch (Exception $e) {
- printf("Message = %s",$e->__toString());
- }
- ?> (fblww-0305)