浅谈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的栏目导航程序
Oct 09 PHP
php源码加密 仿微盾PHP加密专家(PHPCodeLock)
May 06 PHP
php pki加密技术(openssl)详解
Jul 01 PHP
php教程之phpize使用方法
Feb 12 PHP
PHP启动windows应用程序、执行bat批处理、执行cmd命令的方法(exec、system函数详解)
Oct 20 PHP
教大家制作简单的php日历
Nov 17 PHP
php生成mysql的数据字典
Jul 07 PHP
PHP针对多用户实现更换头像功能
Sep 04 PHP
一个简单的php MVC留言本实例代码(必看篇)
Sep 22 PHP
[原创]PHP global全局变量经典应用与注意事项分析【附$GLOBALS用法对比】
Jul 12 PHP
php实现分页功能的详细实例方法
Sep 29 PHP
phpQuery采集网页实现代码实例
Apr 02 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
php中的Base62类(适用于数值转字符串)
2013/08/12 PHP
yii的CURD操作实例详解
2014/12/04 PHP
phpMyAdmin安装并配置允许空密码登录
2015/07/04 PHP
WordPress中缩略图的使用以及相关技巧
2015/11/24 PHP
学习php设计模式 php实现模板方法模式
2015/12/08 PHP
php生成复杂验证码(倾斜,正弦干扰线,黏贴,旋转)
2018/03/12 PHP
jQuery 名称冲突的解决方法
2011/04/08 Javascript
js实现上传图片之上传前预览图片
2013/03/25 Javascript
setInterval()和setTimeout()的用法和区别示例介绍
2013/11/17 Javascript
jquery统计用户选中的复选框的个数
2014/06/06 Javascript
$(&quot;&quot;).click与onclick的区别示例介绍
2014/09/25 Javascript
Jquery使用css方法改变样式实例
2015/05/18 Javascript
分享js粘帖屏幕截图到web页面插件screenshot-paste
2020/08/21 Javascript
利用JQuery实现datatables插件的增加和删除行功能
2017/01/06 Javascript
原生js实现吸顶效果
2017/03/13 Javascript
node打造微信个人号机器人的方法示例
2018/04/26 Javascript
Vue中使用方法、计算属性或观察者的方法实例详解
2018/10/31 Javascript
利用Python如何生成hash值示例详解
2017/12/20 Python
python range()函数取反序遍历sequence的方法
2018/06/25 Python
Django REST framework视图的用法
2019/01/16 Python
对django的User模型和四种扩展/重写方法小结
2019/08/17 Python
python使用requests库爬取拉勾网招聘信息的实现
2020/11/20 Python
改变生活的男士内衣:SAXX Underwear
2019/08/28 全球购物
Noon埃及:埃及在线购物
2019/11/26 全球购物
化学教师自荐信范文
2013/12/28 职场文书
《老王》教学反思
2014/02/23 职场文书
好学生评语大全
2014/05/05 职场文书
教师学期个人总结
2015/02/11 职场文书
2015年家长学校工作总结
2015/04/22 职场文书
老员工辞职信范文
2015/05/12 职场文书
2015年小学财务工作总结
2015/07/20 职场文书
2016教师廉洁从教心得体会
2016/01/13 职场文书
一定要知道的 25 个 Vue 技巧
2021/11/02 Vue.js
【海涛DOTA解说】EVE女子战队独家录像加ZSMJ神牛两连发
2022/04/01 DOTA
nginx之queue的具体使用
2022/06/28 Servers
Springboot集成kafka高级应用实战分享
2022/08/14 Java/Android