浅谈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抓取https的内容的代码
Apr 06 PHP
ThinkPHP与PHPExcel冲突解决方法
Aug 08 PHP
php 网上商城促销设计实例代码
Feb 17 PHP
PHP启动windows应用程序、执行bat批处理、执行cmd命令的方法(exec、system函数详解)
Oct 20 PHP
PHP 二维数组和三维数组的过滤
Mar 16 PHP
Zend Framework框架实现类似Google搜索分页效果
Nov 25 PHP
php实现遍历文件夹的方法汇总
Mar 02 PHP
PHP实现生成模糊图片的方法示例
Dec 21 PHP
Yaf框架封装的MySQL数据库操作示例
Mar 06 PHP
使用Laravel中的查询构造器实现增删改查功能
Sep 03 PHP
浅谈laravel5.5 belongsToMany自身的正确用法
Oct 17 PHP
php变量与字符串的增删改查操作示例
May 07 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输出多个元素的排列或组合的方法
2017/03/14 PHP
Laravel使用PHPQRCODE实现生成带有LOGO的二维码图片功能示例
2017/07/07 PHP
自己动手制作jquery插件之自动添加删除行功能介绍
2011/10/14 Javascript
《JavaScript DOM 编程艺术》读书笔记之JavaScript 简史
2015/01/09 Javascript
JavaScript中的call方法和apply方法使用对比
2015/08/12 Javascript
学习JavaScript设计模式(多态)
2015/11/25 Javascript
jQuery实现获取隐藏div高度的方法示例
2017/02/09 Javascript
JS实现简易刻度时钟示例代码
2017/03/11 Javascript
jQuery.Ajax()的data参数类型详解
2017/07/23 jQuery
基于Vue中点击组件外关闭组件的实现方法
2018/03/06 Javascript
JS实现的贪吃蛇游戏案例详解
2019/05/01 Javascript
在JavaScript中使用严格模式(Strict Mode)
2019/06/13 Javascript
JS关闭子窗口并且刷新上一个窗口的实现示例
2020/03/10 Javascript
js实现简单的无缝轮播效果
2020/09/05 Javascript
详解vue中在父组件点击按钮触发子组件的事件
2020/11/13 Javascript
Python语言编写电脑时间自动同步小工具
2013/03/08 Python
闭包在python中的应用之translate和maketrans用法详解
2014/08/27 Python
Python实现简单多线程任务队列
2016/02/27 Python
Python中getpass模块无回显输入源码解析
2018/01/11 Python
Python递归实现汉诺塔算法示例
2018/03/19 Python
Python采集猫眼两万条数据 对《无名之辈》影评进行分析
2018/12/05 Python
解决Django中调用keras的模型出现的问题
2019/08/07 Python
利用PyCharm操作Github(仓库新建、更新,代码回滚)
2019/12/18 Python
Python的对象传递与Copy函数使用详解
2019/12/26 Python
在django admin详情表单显示中添加自定义控件的实现
2020/03/11 Python
Pytorch - TORCH.NN.INIT 参数初始化的操作
2021/02/27 Python
切尔西足球俱乐部官方网上商店:Chelsea FC
2019/06/17 全球购物
英国拖鞋购买网站:Bedroom Athletics
2020/02/28 全球购物
服务生自我鉴定
2014/01/22 职场文书
敬老院院长事迹材料
2014/05/21 职场文书
中国梦演讲稿3分钟
2014/08/19 职场文书
介绍信的格式
2015/01/30 职场文书
交通安全温馨提示语
2015/07/14 职场文书
办公室规章制度范本
2015/08/04 职场文书
小学教师教学随笔
2015/08/14 职场文书
eclipse创建项目没有dynamic web的解决方法
2021/06/24 Java/Android