php webservice
发请求客户端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()。
*/ //==============================实战链接xml===================================================== //假如给定了一个xml请求和响应示例,要按此规范向服务器发送请求及接收返回信息
//以下是 SOAP 1.2 xml请求示例。如发送请求的内容也是xml,则将占位符(string)替换为实际值即可。
/*
POST /ws_member.asmx HTTP/1.1
Host: 141.207.55.190
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length <?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<soap12:Body>
<UpdatePassword xmlns="http://tempuri.org/">
<strCallUserCode>string</strCallUserCode>
<strCallPassword>string</strCallPassword>
<strMemberCode>string</strMemberCode>
<strOrgPassword>string</strOrgPassword>
<strNewPassword>string</strNewPassword>
</UpdatePassword>
</soap12:Body>
</soap12:Envelope>
*/ //以下是 SOAP 1.2 响应示例,即请求后服务器返回信息内容
/*
HTTP/1.1 200 OK
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length <?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<soap12:Body>
<UpdatePasswordResponse xmlns="http://tempuri.org/">
<UpdatePasswordResult>string</UpdatePasswordResult>
</UpdatePasswordResponse>
</soap12:Body>
</soap12:Envelope>
*/ //将更改占位符后的xml保存到变量$xmldata中
$xmldata = <<<EOT
POST /ws_member.asmx HTTP/1.1
Host: 141.207.55.190
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length <?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<soap12:Body>
<UpdatePassword xmlns="http://tempuri.org/">
<strCallUserCode>TEST</strCallUserCode>
<strCallPassword>123456</strCallPassword>
<strMemberCode>01201201103</strMemberCode>
<strOrgPassword>1234</strOrgPassword>
<strNewPassword>123</strNewPassword>
</UpdatePassword>
</soap12:Body>
</soap12:Envelope>
EOT; //wsdl地址
$wsdl = 'http://141.207.55.190:17654/ws_member.asmx?WSDL';
try{
$client = new SoapClient($wsdl);
//var_dump ( $client->__getTypes() );//获取服务器上数据类型
//var_dump ( $client->__getFunctions() );//获取服务器上提供的方法 //方法一,发送xml请求(这里未成功连接获取信息)
//发送xml必须使用__doRequest,
//$result = $client->__doRequest($xmldata,$wsdl,'UpdatePassword',2);
//print_r($result);
//exit; //方法二,根据xml格式获取请求方法名,及将xml数据转成对应格式数组,传递信息,实测成功
/*
$data = array();
$data['strCallUserCode'] = 'TEST';
$data['strCallPassword'] = '123456';
$data['strMemberCode'] = '01201201103';
$data['strOrgPassword'] = '1234';
$data['strNewPassword'] = '123';
echo '修改密码:';
$result = $client->UpdatePassword($data);
var_dump($result);echo "<br/><br/>";
exit;
*/
}catch (SoapFault $e){
echo $e->getMessage();
}catch(Exception $e){
echo $e->getMessage();
}
?>
服务器端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的更多相关文章
- webService
什么是webService WebService,顾名思义就是基于Web的服务.它使用Web(HTTP)方式,接收和响应外部系统的某种请求.从而实现远程调用. 1:从WebService的工作模式上 ...
- 开始webservice了
一.WebService到底是什么 一言以蔽之:WebService是一种跨编程语言和跨操作系统平台的远程调用技术. 所谓跨编程语言和跨操作平台,就是说服务端程序采用java编写,客户端程序则可以采用 ...
- Spring WebService入门
Web service是一个平台独立的,低耦合的,自包含的.基于可编程的web的应用程序,可使用开放的XML(标准通用标记语言下的一个子集)标准来描述.发布.发现.协调和配置这些应用程序,用于开发分布 ...
- 浅谈跨域以及WebService对跨域的支持
跨域问题来源于JavaScript的同源策略,即只有 协议+主机名+端口号 (如存在)相同,则允许相互访问.也就是说JavaScript只能访问和操作自己域下的资源,不能访问和操作其他域下的资源. 在 ...
- 浅谈WebService的版本兼容性设计
在现在大型的项目或者软件开发中,一般都会有很多种终端, PC端比如Winform.WebForm,移动端,比如各种Native客户端(iOS, Android, WP),Html5等,我们要满足以上所 ...
- Atitit webservice发现机制 WS-Discovery标准的规范attilax总结
Atitit webservice发现机制 WS-Discovery标准的规范attilax总结 1.1. WS-Discovery标准1 1.2. 一.WS-Discovery1 1.2.1. ...
- java调用CXF WebService接口的两种方式
通过http://localhost:7002/card/services/HelloWorld?wsdl访问到xml如下,说明接口写对了. 2.静态调用 // 创建WebService客户端代理工厂 ...
- VS2010编写WebService与在IIS的发布<之简单讲解>
工具VS2010,window环境win7 一:Webservice的创建与方法查看调用 1.新建空web应用程序项目 2.新建web服务 3.自动生成 4.直接跑起来,可以看到有2个方法 5.点击H ...
- webService学习之路(三):springMVC集成CXF后调用已知的wsdl接口
webService学习之路一:讲解了通过传统方式怎么发布及调用webservice webService学习之路二:讲解了SpringMVC和CXF的集成及快速发布webservice 本篇文章将讲 ...
- webService学习之路(二):springMVC集成CXF快速发布webService
继上一篇webService入门之后,http://www.cnblogs.com/xiaochangwei/p/4969448.html ,现在我将我周六在家研究的结果公布出来 本次集成是基于之前已 ...
随机推荐
- uploadfile图片上传和ashx
uploadify.swf需要是支持中文 $(function () { //获取所有上传按钮id $("div[id^='fileInput_']").each(fu ...
- 一篇UI规范文件
一篇UI规范文件 这是一个UI模板规范,在做B/S版应用程序时比较适用,其实这样的东西算不上什么正规的规范,只是为了适应我们现在面对的开发环境和组织流程做的一些权宜的努力,和解决了一些与程序沟通和接口 ...
- NOI上看到的几个小学奥数
:余数相同问题 查看 提交 统计 提问 总时间限制: 1000ms 内存限制: 65536kB 描述 已知三个正整数 a,b,c. 现有一个大于1的整数x,将其作为除数分别除a,b,c,得到的余数相同 ...
- Java中MyEclipse快捷键整理
************************************ MyEclipse 快捷键1(CTRL) ************************************ Ctrl ...
- Linux定时任务Crontab详解
原文地址:http://edu.codepub.com/2011/0104/28518.php 今天做了个数据库的备份脚本,顺便系统得学习一下Linux下定时执行脚本的设置.Linux下的定 ...
- 基于Redis的爬虫平台的实现
一.需求: 1.数据抓取:目标数据的下载.解析.入库功能. 2.数据服务:黑名单.灰名单等查询服务. 3.平台监控:平台各个模块的数据实时监控. 二.WEB端效果展示: 三.架构设计 下载器.解析器. ...
- log4j+mongodb
maven 配置: <dependency> <groupId>org.mongodb</groupId> <artifactId>mongo-java ...
- Vimium使用快捷键总结
chrome 快捷键: ctrl+w 关闭当前标签 ctrl+t 新建标签 gg行首 shift+g 行尾 Vimium使用快捷键总结 j, <c-e> : Scroll down k, ...
- CentOS 7中防火墙 firewall-cmd命令
在 CentOS 7 iptable 防火墙已经被 firewall替代 1.暂时开放FTP服务 firewall-cmd --add-service=ftp 2.永久开放FTP服务 firewall ...
- Docker的容器
容器是一个打包了应用和服务的环境,是一个轻量级的虚拟机,每一个容器都由一组特定的应用和必要的依赖库组成. 容器的管理操作 容器常见的命令:查看.创建.启动.终止和删除 创建容器 docker crea ...