浅谈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 相关文章推荐
PHP6 先修班 JSON实例代码
Aug 23 PHP
PHP伪造referer实例代码
Sep 20 PHP
php中http_build_query 的一个问题
Mar 25 PHP
PHP通过插入mysql数据来实现多机互锁实例
Nov 05 PHP
php函数连续调用实例分析
Jul 30 PHP
php中ob函数缓冲机制深入理解
Aug 03 PHP
php 使用curl模拟登录人人(校内)网的简单实例
Jun 06 PHP
PHP  实现等比压缩图片尺寸和大小实例代码
Oct 08 PHP
php静态成员方法和静态的成员属性的使用方法
Oct 26 PHP
php JWT在web端中的使用方法教程
Sep 06 PHP
PHP Swoole异步读取、写入文件操作示例
Oct 24 PHP
laravel框架中控制器的创建和使用方法分析
Nov 23 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简单静态页生成过程
2008/03/27 PHP
PHP简单系统数据添加以及数据删除模块源文件下载
2008/06/07 PHP
php设计模式 Mediator (中介者模式)
2011/06/26 PHP
ThinkPHP无限级分类原理实现留言与回复功能实例
2014/10/31 PHP
PHP中调用SVN命令更新网站方法
2015/01/07 PHP
php的ddos攻击解决方法
2015/01/08 PHP
php实现通用的信用卡验证类
2015/03/24 PHP
SWFObject 2.1以上版本语法介绍
2010/07/10 Javascript
jquery中获取select选中值的代码
2011/06/27 Javascript
30个精美的jQuery幻灯片效果插件和教程
2011/08/23 Javascript
jquery动态增加text元素以及删除文本内容实例代码
2013/07/01 Javascript
深入解析contentWindow, contentDocument
2013/07/04 Javascript
jquery内置验证(validate)使用方法示例(表单验证)
2013/12/04 Javascript
javascript和jquery实现用户登录验证
2016/05/04 Javascript
JavaScript中的原型prototype完全解析
2016/05/10 Javascript
JavaScript判断数组是否存在key的简单实例
2016/08/03 Javascript
js手动播放图片实现图片轮播效果
2016/09/17 Javascript
nodejs实现OAuth2.0授权服务认证
2017/12/27 NodeJs
vue mint-ui tabbar变组件使用
2018/05/04 Javascript
微信小程序实现图片滚动效果示例
2018/12/05 Javascript
javascript canvas API内容整理
2020/02/16 Javascript
react结合bootstrap实现评论功能
2020/05/30 Javascript
Pycharm设置去除显示的波浪线方法
2018/10/28 Python
在Python中给Nan值更改为0的方法
2018/10/30 Python
Python3.4学习笔记之常用操作符,条件分支和循环用法示例
2019/03/01 Python
python实现列表中最大最小值输出的示例
2019/07/09 Python
使用 Python 处理 JSON 格式的数据
2019/07/22 Python
在线服装零售商:SheIn
2016/07/22 全球购物
最耐用行李箱,一箱永流传:Briggs & Riley(全球终身保修)
2017/12/07 全球购物
印尼在线购买隐形眼镜网站:Lensza.co.id
2019/04/27 全球购物
添柏岚英国官方网站:Timberland英国
2019/11/28 全球购物
adidas泰国官网:adidas TH
2020/07/11 全球购物
Linux如何修改文件和文件夹的权限
2013/09/05 面试题
Python中os模块的简单使用及重命名操作
2021/04/17 Python
Java 实现限流器处理Rest接口请求详解流程
2021/11/02 Java/Android
java高级用法JNA强大的Memory和Pointer
2022/04/19 Java/Android