用PHP实现webservice,有二种方式:
1,用PHP扩展soap
2,用开源软件nusoap
注:php5内置了soap扩展,php5之前若用soap扩展,需要到PEAR(the PHP Extension and Application Repository)下载。
这里讲解扩展soap的实现方式。
准备工作(配置php.ini):
1,取消extension=php_soap.dll前面的分号;
2,修改soap.wsdl_cache_enabled = 1 为soap.wsdl_cache_enabled = 0(该参数提供WSDL文件缓存,在运行环境中设置1,使用缓存;在调试环境中设置为0,不使用缓存)。
修改php.ini后要重启apache服务器。
non-wsdl模式:
服务器端:SoapHello1.php
<?php //实例化SOAP服务 $server = new SoapServer(null, //non-WSDL模式,不指定WSDL文件 array('uri' => 'www.kingdee.com', 'soap_version' => SOAP_1_2)); //注册提供外部调用的方法 $server->addFunction('hello'); //可以注册方法,也可以注册类: //$server->setClass("class name"); $server->handle(); //注册方法的实现 function hello($name, $password) { if ($password == 'lory' && $name == 'lory') { return 'Welcome lory, how are you?'; } else { return 'Go away!!!'; } } exit(); ?>
该实现中没有支持WSDL,因此也无法将服务器提供的接口暴露。在浏览器中访问服务端文件:
客户端soapclient1.php:
<?php try { //实例化客户端 /*If working in WSDL mode, this parameter is optional. If working in non-WSDL mode, the location and uri options must be set, where location is the URL to request and uri is the target namespace of the SOAP service. */ $client = new SoapClient(null, // non-WSDL模式,不指定WSDL文件 array('location' =>"http://192.168.69.241/MyPHP/SoapHello1.php?wsdl", 'uri' => "http://www.kingdee.com/")); //调用服务端方法,并打印出返回结果 echo $client->hello('lory','lory'); } catch (SoapFault $fault){ echo "Error: ",$fault->faultcode,", string: ",$fault->faultstring; } exit(); ?>
浏览器访问客户端,结果如下:
WSDL模式:
预先生成WSDL文件wsdl/hello.wsdl,该文件可以手写或工具生成(这个WSDL文件实际是由NuSOAP时WSDL模式生成的):
<?xml version="1.0" encoding="ISO-8859-1" ?> <definitions xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:tns="http://www.sugarcrm.com/sugarcrm" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://www.sugarcrm.com/sugarcrm"> <types> <xsd:schema targetNamespace="http://www.sugarcrm.com/sugarcrm"> <xsd:import namespace="http://schemas.xmlsoap.org/soap/encoding/" schemaLocation="http://schemas.xmlsoap.org/soap/encoding/" /><xsd:import namespace="http://schemas.xmlsoap.org/wsdl/" schemaLocation="http://schemas.xmlsoap.org/soap/encoding/" /> </xsd:schema> </types> <message name="helloRequest"> <part name="user_name" type="xsd:string" /> <part name="password" type="xsd:string" /> </message> <message name="helloResponse"> <part name="return" type="xsd:string" /> </message><input> <soap:body use="encoded" namespace="http://www.sugarcrm.com/sugarcrm" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" /> </input> <output> <soap:body use="encoded" namespace="http://www.sugarcrm.com/sugarcrm" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" /> </output> </operation> </binding> <service name="sugarsoap"> <port name="sugarsoapPort" binding="tns:sugarsoapBinding"> <soap:address location="http://localhost/sugarcrm/SoapLoryTest.php" /> </port> </service></definitions>
服务器端:SoapHello1.php
<?php //实例化SOAP服务 $server = new SoapServer('wsdl/hello.wsdl', //指定WSDL文件,预先生成 array('soap_version' => SOAP_1_2)); //注册提供外部调用的方法 $server->addFunction('hello'); $server->handle(); //注册方法的实现 function hello($name, $password) { if ($password == 'lory' && $name == 'lory') { return 'Welcome lory, how are you?'; } else { return 'Go away!!!'; } } exit(); ?>
在浏览器中访问服务端文件,http://192.168.69.241/MyPHP/SoapHello1.php?wsdl, 可以看到其向外发布的WSDL文件定义信息:
其客户端调用与non-WSDL基本相同,在实例化SoapClient时改为:
$client = new SoapClient('http://192.168.69.241/MyPHP/SoapHello1.php?wsdl');
参考:
http://www.cnblogs.com/chance1/archive/2009/04/08/1431949.html
http://www.ibm.com/developerworks/cn/webservices/1003_chenchen_phpws/
http://hi.baidu.com/chesterphp/item/e61ad8f5733b1d2b743c4c07