基于wsdl,使用soap实现webservice

    实现webservice的方式多多。这里我介绍一下使用PHP自带的soap实现。当然首先要保证在php扩展配置中开启soap.

步骤一:修改php.ini。添加extension=php_soap.dll来加载soap内置包。(或者去掉前面的“;”)

步骤二:写服务端文件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文件。目前生成该类型文件有两种方式。1、使用soapui在zend编辑器中生成。2、使用辅助类SoapDiscovery.class.php来生成。正如代码所示。我们选择的是使用后者。我们允许s.php这个文件。  WSDL文件不存在时自动创建。然后$server = new SoapServer(WSDL_URL);创建了服务端并且将MyTest类放了进去。

     SoapDiscovery.class.php文件下载地址(其中包含例子)http://download.csdn.net/detail/wingth11/5641491

生成的hello.wsdl文件如下所示:

<?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.php

<?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://开头,否则不能实例化成功。运行该文件,结果如下:

array (size=2) 0 => string'string fun1()' (length=13) 1 => string'string fun2(string $name)' (length=25)
string'this is fun1()' (length=14)

 

 


 


转载于:https://my.oschina.net/u/214052/blog/395781

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值