码迷,mamicode.com
首页 > Web开发 > 详细

php webservice

时间:2016-08-28 20:53:39      阅读:154      评论:0      收藏:0      [点我收藏+]

标签:

发请求客户端client.php

<?php
//需要到php.ini文件中打开extension=php_soap.dll
try{
    //wsdl方式调用web service
    //wsdl方式中由于wsdl文件写定了,如果发生添加删除函数等操作改动,不会反应到wsdl,相对non-wsdl方式来说不够灵活
    //$soap = new SoapClient("http://127.0.0.1/test/test.wsdl");
    
    //non-wsdl方式调用web service,没有要调用的wsdl文件,所以第一参数为空
    //location webservice服务路径的地址
    //uri      要用要调用的webserivce中的uri一致
    $soap = new SoapClient(null, array(‘location‘=>‘http://127.0.0.1/test/server.php‘, ‘uri‘ =>‘test_php_webservice‘));
    
    
    echo $soap->fun1(array(1,2,3));
    //echo $soap->fun2(123);
    
    //wsdl方式wsdl文件中未定义fun3,fun4方法,所以此处只能以non-wsdl方式调用
    //echo $soap->fun3(‘sss‘);
    //echo $soap->fun4(‘aaa‘);
}catch(SoapFault $fault){
    //可以使用try catch也可以不用,使用try catch错误信息会自动记录到SoapFault类对象中
    echo $fault->getMessage();;
}catch(Exception $e){
    echo $e->getMessage();
}

/*
SoapClient类

这个类用来使用Web services。SoapClient类可以作为给定Web services的客户端。
它有两种操作形式:

* WSDL 模式
* Non-WSDL 模式

在WSDL模式中,构造器可以使用WSDL文件名作为参数,并从WSDL中提取服务所使用的信息。

non-WSDL模式中使用参数来传递要使用的信息。

SoapServer类

这个类可以用来提供Web services。与SoapClient类似,SoapServer也有两种操作模式:WSDL模式和non-WSDL模式。这两种模式的意义跟 SoapClient的两种模式一样。在WSDL模式中,服务实现了WSDL提供的接口;在non-WSDL模式中,参数被用来管理服务的行为。

在SoapServer类的众多方法中,有三个方法比较重要。它们是SoapServer::setClass(),SoapServer::addFunction()和SoapServer::handle()。
*/
?>

服务器端server.php

<?php
class TestClass {
    function fun1($arg1) {
        return json_encode($arg1);
    }
    function fun2($arg2) {
        return $arg2;
    }
}
function fun3($arg3) {
    return $arg3;
}
function fun4($arg4) {
    return $arg4;
}

//wsdl方式提供web service,如果生成了wsdl文件则可直接传递到//SoapServer的构造函数中
//ini_set(‘soap.wsdl_cache_enabled‘,‘0‘);//关闭WSDL缓存
//$server = new SoapServer(‘http://127.0.0.1/test/test.wsdl‘);
/*
已有现成的简单wsdl文件,需要使用时可在此基础上简单修改使用
1,把所有test_php_webservice换成自定义名称
2,把类名TestClass换成实际所需类名
3,把方法名fun1,fun2换成实际所用方法名,及参数,返回值类型,也可按已有fun1,fun2标签格式新增所需方法
4,把http://127.0.0.1:80/test/server.php换成实际服务器端处理路径
*/



//non-wsdl方式这个是没有使用wsdl文件的,所以第一个参数为null,如果有使用wsdl,那么第一个参数就是这个wsdl文件的地址。
//uri          相当于命名空间,可以是任何不和别人重合的字符串
//soap_version 表示soap的版本号,目前就两个版本号SOAP_1_1,SOAP_1_2
$server = new SoapServer ( null, array ( ‘uri‘ => ‘test_php_webservice‘, ‘soap_version‘ => SOAP_1_2 ) );



// setClass和addFunction不能同时设置,addFunction可以添加多个方法,设置的setClass类里面的方法或者addFunction添加的方法,可以在客户端直接通过$soap对象->方法名调用
$server->setClass ( ‘TestClass‘ );

//addFunction只适用于non-wsdl方式,wsdl方式可访问方法都是在wsdl中固定的
//$server->addFunction(‘fun3‘);
//$server->addFunction(‘fun4‘);
$server->handle ();
?>

test.wsdl文件

<?xml version="1.0" ?>
<definitions name="test_php_webservice" targetNamespace="urn:test_php_webservice" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="urn:test_php_webservice" 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="test_php_webservicePort">

<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="test_php_webserviceBinding" type="tns:test_php_webservicePort">
<soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http" />

<operation name="fun1">
<soap:operation soapAction="urn:test_php_webservice#TestClass#fun1" />
<input>
<soap:body use="encoded" namespace="urn:test_php_webservice" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
</input>
<output>
<soap:body use="encoded" namespace="urn:test_php_webservice" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
</output>
</operation>

<operation name="fun2">
<soap:operation soapAction="urn:test_php_webservice#TestClass#fun2" />
<input>
<soap:body use="encoded" namespace="urn:test_php_webservice" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
</input>
<output>
<soap:body use="encoded" namespace="urn:test_php_webservice" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
</output>
</operation>

</binding>
<service name="test_php_webservice">
<documentation />
<port name="test_php_webservicePort" binding="tns:test_php_webserviceBinding"><soap:address location="http://127.0.0.1:80/test/server.php" />
</port>
</service>

<message name="fun1Request">
<part name="name" type="xsd:array" />
</message>
<message name="fun1Response">
<part name="fun1" type="xsd:array" />
</message>

<message name="fun2Request">
<part name="name" type="xsd:array" />
</message>
<message name="fun2Response">
<part name="fun2" type="xsd:string" />
</message>

</definitions>

 

php webservice

标签:

原文地址:http://www.cnblogs.com/dreamhome/p/5815749.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!