用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
运维网声明
1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网 享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com