php web service(基于soap)

来源:互联网 发布:linux创建文本文件命令 编辑:程序博客网 时间:2024/05/14 08:50

最近项目要用web service,所以采取php来实现web service

首先开启php自带的soap扩展,在php.ini文件中去掉;注释 

;extension=php_soap.dll 这行,去掉注释符“;”,保存并重启 httpd 服务。(Asianux linux server 3自带php已开启soap扩展)

web service有两种实现模式:

1.契约先行方式

首要工作是定义针对此web服务的接口 WSDL 文件。WSDL文件描述了服务的位置,可提供的操作集,以及一些其他属性。

2.代码先行模式

第一步工作是实现WEB服务端,然后根据服务端的实现,用某种方法(自动或手动编写)生成WSDL文件。由于PHP不能直接生成WSDL文件,所以采用NON-WSDL模式连接服务端,直接向构造函数传递必要的参数。

php soap扩展实现了6个类,三个高级类分别为SoapServer、SoapClient、SoapFault)三个低级类(除构造函数外没有其他方法)分别为SoapHeader、SoapParam和SoapVar


SoapServer类用来提供web service,在WSDL模式中服务实现了WSDL提供的接口,在NON-WSDL模式中,参数被用来管理服务的行为。

SoapClient类用来创建客户端实例,调用可用操作、查询可用操作和数据类型等,还包括可用于程序调试的函数。

SoapFault类继承自php的Exception,可用力啊实现SOAP中的异常处理机制,由SOAP服务端抛出,SOAP客户端可以接收该类的实例,用于获取有用的信息。

SoapParam类 和 SoapVar类主要用来封装用于放在SOAP请求中的数据,主要在NON-WSDL模式下用。在WSDL模式下,请求的参数可以通过数组方式包装。SOAP扩展回更具WSDL文件将这个数组转化为SOAP请求的数据部分,所以并不需要这两个类。

SoapHeader类用来构造SOAP头,SOAP头可以对SOAP的能力进行必要的扩展。SOAP头的主要作用是用于简单的身份认证。

标准web service(wsdl模式)创建过程:

1.生成服务的类文件myservice.php

<?phpclass service{public  function Add($a,$b){return $a+$b;}}?>




2.生成WSDL文件。

首先需要一个SoapDiscovery.class.php文件(该文件是网友提供根据SoapDiscovery类自动生成WSDL文件的php文件。可从网上下载)。

然后自定义文件wsdl.php并调用刚才的SoapDiscovery

<?php include("myservice.php"); include("SoapDiscovery.class.php"); $disco = new SoapDiscovery('service','service');//第一个参数是类名(生成的wsdl文件就是以它来命名的),即person类,第二个参数是服务的名字(这个可以随便写)。 $disco->getWSDL(); ?> 

   在浏览器访问wsdl.php即可生成相关WSDL文件。

<?xml version="1.0" ?><definitions name="service" targetNamespace="urn:service" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="urn:service" 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="servicePort"><operation name="HelloWorld"><input message="tns:HelloWorldRequest" /><output message="tns:HelloWorldResponse" /></operation><operation name="Add"><input message="tns:AddRequest" /><output message="tns:AddResponse" /></operation></portType><binding name="serviceBinding" type="tns:servicePort"><soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http" /><operation name="HelloWorld"><soap:operation soapAction="urn:service#service#HelloWorld" /><input><soap:body use="encoded" namespace="urn:service" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" /></input><output><soap:body use="encoded" namespace="urn:service" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" /></output></operation><operation name="Add"><soap:operation soapAction="urn:service#service#Add" /><input><soap:body use="encoded" namespace="urn:service" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" /></input><output><soap:body use="encoded" namespace="urn:service" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" /></output></operation></binding><service name="service"><documentation /><port name="servicePort" binding="tns:serviceBinding"></span><strong style="font-size:18px;"><soap:address location="http://localhost:80/wsdl.php" /></strong></port></service><message name="HelloWorldRequest"></message><message name="HelloWorldResponse"><part name="HelloWorld" type="xsd:string" /></message><message name="AddRequest"><part name="a" type="xsd:string" /><part name="b" type="xsd:string" /></message><message name="AddResponse"><part name="Add" type="xsd:string" /></message></definitions>

将localtion改为服务的地址location="http://10.75.57.71:80/webs/myservice.php"

创建web服务myservice.php

<?phpclass service{public  function Add($a,$b){return $a+$b;}}$server=new SoapServer('service.wsdl');$server->setClass("service");$server->handle();?>


创建SOAP客户端

<?php$a=new SoapClient('http://10.75.58.71/webs/myservice.php?wsdl');try{echo $a->Add(14,2);}catch(Exception $e){  print_r($e);  }?>

至此web service已部署完毕





0 0
原创粉丝点击