浅谈PHP调用Webservice思路及源码分享


Posted in PHP onJune 04, 2014

方法一:直接调用

<?  
/******************************************************************************/ 
/*  文件名 : soapclient.php 
/*  说  明 : WebService接口客户端例程 
/******************************************************************************/ 
include('NuSoap.php');  // 创建一个soapclient对象,参数是server的WSDL   
$client = new soapclient('http://localhost/Webservices/Service.asmx?WSDL', 'wsdl');  
// 参数转为数组形式传递  
$aryPara = array('strUsername'=>'username', 'strPassword'=>MD5('password'));  
// 调用远程函数  
$aryResult = $client->call('login',$aryPara);  
//echo $client->debug_str;  
/* 
if (!$err=$client->getError()) { 
  print_r($aryResult);  
} else {  
  print "ERROR: $err";  
} 
*/ 
$document=$client->document;  
echo <<<SoapDocument  
<?xml version="1.0" encoding="GB2312"?>  
 <SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" 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:si="http://soapinterop.org/xsd">  
   <SOAP-ENV:Body>  
   $document 
   </SOAP-ENV:Body>  
 </SOAP-ENV:Envelope>  
SoapDocument;  
?> 
<?
/******************************************************************************/
/*  文件名 : soapclient.php
/*  说  明 : WebService接口客户端例程
/******************************************************************************/
include('NuSoap.php');
// 创建一个soapclient对象,参数是server的WSDL
$client = new soapclient('http://localhost/Webservices/Service.asmx?WSDL', 'wsdl');
// 参数转为数组形式传递
$aryPara = array('strUsername'=>'username', 'strPassword'=>MD5('password'));
// 调用远程函数
$aryResult = $client->call('login',$aryPara);
//echo $client->debug_str;
/*
if (!$err=$client->getError()) {
  print_r($aryResult);
} else {
  print "ERROR: $err";
}
*/
$document=$client->document;
echo <<<SoapDocument
<?xml version="1.0" encoding="GB2312"?>
 <SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" 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:si="http://soapinterop.org/xsd">
   <SOAP-ENV:Body>
   $document
   </SOAP-ENV:Body>
 </SOAP-ENV:Envelope>
SoapDocument;
?>

方法二:代理方式调用

<?  
/******************************************************************************/ 
/*  文件名 : soapclient.php 
/*  说  明 : WebService接口客户端例程 
/******************************************************************************/ 
require('NuSoap.php');   //创建一个soapclient对象,参数是server的WSDL   
$client=new soapclient('http://localhost/Webservices/Service.asmx?WSDL', 'wsdl');   
//生成proxy类   
$proxy=$client->getProxy();   
//调用远程函数   
$aryResult=$proxy->login('username',MD5('password'));  
//echo $client->debug_str;  
/* 
if (!$err=$proxy->getError()) { 
  print_r($aryResult);  
} else {  
  print "ERROR: $err";  
} 
*/ 
$document=$proxy->document;  
echo <<<SoapDocument  
<?xml version="1.0" encoding="GB2312"?>  
 <SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" 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:si="http://soapinterop.org/xsd">  
   <SOAP-ENV:Body>  
   $document 
   </SOAP-ENV:Body>  
 </SOAP-ENV:Envelope>  
SoapDocument;  
?> 
<?
/******************************************************************************/
/*  文件名 : soapclient.php
/*  说  明 : WebService接口客户端例程
/******************************************************************************/
require('NuSoap.php');
//创建一个soapclient对象,参数是server的WSDL
$client=new soapclient('http://localhost/Webservices/Service.asmx?WSDL', 'wsdl');
//生成proxy类
$proxy=$client->getProxy();
//调用远程函数
$aryResult=$proxy->login('username',MD5('password'));
//echo $client->debug_str;
/*
if (!$err=$proxy->getError()) {
  print_r($aryResult);
} else {
  print "ERROR: $err";
}
*/
$document=$proxy->document;
echo <<<SoapDocument
<?xml version="1.0" encoding="GB2312"?>
 <SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" 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:si="http://soapinterop.org/xsd">
   <SOAP-ENV:Body>
   $document
   </SOAP-ENV:Body>
 </SOAP-ENV:Envelope>
SoapDocument;
?>

许多使用NuSoap 调用.NET WebService或J2EE  WebService的朋友可能都遇到过中文乱码问题,下面介绍这一问题的出现的原因和相应的解决方法。

NuSoap调用WebService出现乱码的原因:

通常我们进行WebService开发时都是用的UTF-8编码,这时我们需要设置:

$client->soap_defencoding = 'utf-8'; 
$client->soap_defencoding = 'utf-8';

同时,需要让xml以同样的编码方式传递:

$client->xml_encoding = 'utf-8'; 
$client->xml_encoding = 'utf-8';

至此应该是一切正常了才对,但是我们在输出结果的时候,却发现返回的是乱码。

NuSoap调用WebService出现乱码的解决方法:

实际上,开启了调试功能的朋友,相信会发现$client->response返回的是正确的结果,为什么$result = $client->call($action, array('parameters' => $param)); 却是乱码呢?

研究过NuSoap代码后我们会发现,当xml_encoding设置为UTF-8时,NuSoap会检测decode_utf8的设置,如果为true,会执行 PHP 里面的utf8_decode函数,而NuSoap默认为true,因此,我们需要设置:

$client->soap_defencoding = 'utf-8';  
$client->decode_utf8 = false;  
$client->xml_encoding = 'utf-8'; 
$client->soap_defencoding = 'utf-8';
$client->decode_utf8 = false;
$client->xml_encoding = 'utf-8';
PHP 相关文章推荐
PHP开发过程中常用函数收藏
Dec 14 PHP
php 字符串函数收集
Mar 29 PHP
PHP 字符串加密函数(在指定时间内加密还原字符串,超时无法还原)
Apr 28 PHP
php 操作调试的方法
Jul 12 PHP
php数组一对一替换实现代码
Aug 31 PHP
php实现转换html格式为文本格式的方法
May 16 PHP
php解决和避免form表单重复提交的几种方法
Aug 31 PHP
PHP编程文件处理类SplFileObject和SplFileInfo用法实例分析
Jul 22 PHP
PHP下 Mongodb 连接远程数据库的实例代码
Aug 30 PHP
php记录搜索引擎爬行记录的实现代码
Mar 02 PHP
YII框架学习笔记之命名空间、操作响应与视图操作示例
Apr 30 PHP
php 使用mpdf实现指定字段配置字体样式的方法
Jul 29 PHP
利用谷歌 Translate API制作自己的翻译脚本
Jun 04 #PHP
PHP函数分享之curl方式取得数据、模拟登陆、POST数据
Jun 04 #PHP
PHP 如何获取二维数组中某个key的集合
Jun 03 #PHP
PHP 二维数组根据某个字段排序的具体实现
Jun 03 #PHP
php 批量添加多行文本框textarea一行一个
Jun 03 #PHP
PHP解决URL中文GBK乱码问题的两种方法
Jun 03 #PHP
php数组中包含中文的排序方法
Jun 03 #PHP
You might like
DISCUZ 分页代码
2007/01/02 PHP
php判断手机访问还是电脑访问示例分享
2014/01/20 PHP
php计算数组不为空元素个数的方法
2014/01/27 PHP
php防止站外远程提交表单的方法
2014/10/20 PHP
thinkPHP+PHPExcel实现读取文件日期的方法(含时分秒)
2016/07/07 PHP
php文件上传原理与实现方法详解
2019/12/20 PHP
利用javascript查看html源文件
2006/11/08 Javascript
封装的原生javascript弹出层代码
2010/09/24 Javascript
jQuery删除当前节点元素
2016/12/07 Javascript
AngularJS路由实现页面跳转实例
2017/03/03 Javascript
jQuery实现鼠标滑过预览图片大图效果的方法
2017/04/26 jQuery
jquery ui sortable拖拽后保存位置
2017/04/27 jQuery
js+css实现红包雨效果
2018/07/12 Javascript
详解小程序用户登录状态检查与更新实例
2019/05/15 Javascript
对layui中的onevent 和event的使用详解
2019/09/06 Javascript
js基础之事件捕获与冒泡原理
2019/10/09 Javascript
Python装饰器入门学习教程(九步学习)
2016/01/28 Python
Android应用开发中Action bar编写的入门教程
2016/02/26 Python
python自动翻译实现方法
2016/05/28 Python
Python实现定时精度可调节的定时器
2018/04/15 Python
Python初学者需要注意的事项小结(python2与python3)
2018/09/26 Python
Python正则匹配判断手机号是否合法的方法
2020/12/09 Python
python使用requests模块实现爬取电影天堂最新电影信息
2019/04/03 Python
python 通过可变参数计算n个数的乘积方法
2019/06/13 Python
Django如何在不停机的情况下创建索引
2020/08/02 Python
澳大利亚儿童鞋在线:The Trybe
2019/07/16 全球购物
学生个人求职自荐信格式
2013/09/23 职场文书
京剧自荐信
2014/01/26 职场文书
幼儿园六一活动总结
2014/08/27 职场文书
幼儿园学前班幼儿评语
2014/12/29 职场文书
初中毕业生感言
2015/07/31 职场文书
2016年度师德标兵先进事迹材料
2016/02/26 职场文书
教师实习自我鉴定总结
2019/08/20 职场文书
导游词之寿县报恩寺
2020/01/19 职场文书
Element-ui Layout布局(Row和Col组件)的实现
2021/12/06 Vue.js
Java 多态分析
2022/04/26 Java/Android