实现webservice的方式多多。这里我介绍一下使用PHP自带的soap实现。固然首先要保证在php扩展配置中开启soap.php
步骤一:修改php.ini。添加extension=php_soap.dll来加载soap内置包。(或者去掉前面的“;”)web
步骤二:写服务端文件s.php缓存
<?php /** * wsdl服务端 */ define('WSDL_URL','hello.wsdl'); //定义WSDL文件路径 ini_set('soap.wsdl_cache_enabled','0'); //关闭WSDL缓存 //WSDL文件不存在时自动建立 if(!file_exists(WSDL_URL)) { require_once 'SoapDiscovery.class.php'; $disco = new SoapDiscovery('MyTest','www.epetbar.com');//第一参数为类名,第二个参数是服务的名字能够随便写 $str = $disco->getWSDL(); file_put_contents(WSDL_URL,$str);//将生成的wsdl字符串保存到当前目录的'hello.wsdl'文件中 } //SOAP开启并接收Client传入的参数响应 $server = new SoapServer(WSDL_URL); $server->setClass('MyTest');//参数为要加载进去的类名 $server->handle(); //测试定义公开的类 class MyTest { public function fun1(){ return 'this is fun1()'; } public function fun2($name){ return 'the name is ' . $name; } } ?>
咱们是基于wsdl实现的。因此咱们要生成wsdl文件。目前生成该类型文件有两种方式。一、使用soapui在zend编辑器中生成。二、使用辅助类SoapDiscovery.class.php来生成。正如代码所示。咱们选择的是使用后者。咱们容许s.php这个文件。 WSDL文件不存在时自动建立。而后$server = new SoapServer(WSDL_URL);建立了服务端而且将MyTest类放了进去。编辑器
SoapDiscovery.class.php文件下载地址(其中包含例子)http://download.csdn.net/detail/wingth11/5641491 测试
生成的hello.wsdl文件以下所示:ui
<?xml version="1.0" ?> <definitions name="www.epetbar.com" targetNamespace="urn:www.epetbar.com" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="urn:www.epetbar.com" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns="http://schemas.xmlsoap.org/wsdl/"> <types xmlns="http://schemas.xmlsoap.org/wsdl/" /> <portType name="www.epetbar.comPort"><operation name="fun1"> <input message="tns:fun1Request" /> <output message="tns:fun1Response" /> </operation> <operation name="fun2"> <input message="tns:fun2Request" /> <output message="tns:fun2Response" /> </operation> </portType> <binding name="www.epetbar.comBinding" type="tns:www.epetbar.comPort"> <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http" /> <operation name="fun1"> <soap:operation soapAction="urn:www.epetbar.com#MyTest#fun1" /> <input><soap:body use="encoded" namespace="urn:www.epetbar.com" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" /> </input> <output> <soap:body use="encoded" namespace="urn:www.epetbar.com" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" /> </output> </operation> <operation name="fun2"> <soap:operation soapAction="urn:www.epetbar.com#MyTest#fun2" /> <input><soap:body use="encoded" namespace="urn:www.epetbar.com" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" /> </input> <output> <soap:body use="encoded" namespace="urn:www.epetbar.com" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" /> </output> </operation> </binding> <service name="www.epetbar.com"> <documentation /> <port name="www.epetbar.comPort" binding="tns:www.epetbar.comBinding"><soap:address location="http://localhost:80/mycode/webservice/s.php" /> </port> </service> <message name="fun1Request"> </message> <message name="fun1Response"> <part name="fun1" type="xsd:string" /> </message> <message name="fun2Request"> <part name="name" type="xsd:string" /> </message> <message name="fun2Response"> <part name="fun2" type="xsd:string" /> </message> </definitions>
步骤三:写客户端文件c.phpthis
<?php /** * wsdl客户端 */ $client = new SoapClient("http://localhost/mycode/webservice/hello.wsdl"); try { var_dump($client->__getFunctions()); $result = $client->fun1(); var_dump($result); } catch (SoapFault $f){ echo "Error Message: {$f->getMessage()}"; } ?>
在上面代码new SoapClient()中的参数是生成的wsdl文件地址。地址中必定要有http://开头,不然不能实例化成功。运行该文件,结果以下:spa
array (size=2) 0 => string'string fun1()' (length=13) 1 => string'string fun2(string $name)' (length=25)
string'this is fun1()' (length=14)