浅谈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中使用Oracle数据库(2)
Oct 09 PHP
PHP 地址栏信息的获取代码
Jan 07 PHP
php 将bmp图片转为jpg等其他任意格式的图片
Jun 21 PHP
PHP取进制余数函数代码
Jan 19 PHP
php跨域cookie共享使用方法
Feb 20 PHP
PHP中使用file_get_contents抓取网页中文乱码问题解决方法
Dec 17 PHP
php中$_POST与php://input的区别实例分析
Jan 07 PHP
Zend Framework分页类用法详解
Mar 22 PHP
PHP智能识别收货地址信息实例
Jan 05 PHP
TP5(thinkPHP5框架)基于bootstrap实现的单图上传插件用法示例
May 29 PHP
阿里对象存储OSS在laravel框架中的使用方法
Oct 13 PHP
实例讲解PHP表单
Jun 10 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长字符串定义方法
2012/07/12 PHP
PHP优于Node.js的五大理由分享
2012/09/15 PHP
PDO版本问题 Invalid parameter number: no parameters were bound
2013/01/06 PHP
PHP定时执行任务实现方法详解(Timer)
2015/07/30 PHP
win7安装php框架Yii的方法
2016/01/25 PHP
php获取文件后缀的9种方法
2016/03/22 PHP
thinkPHP简单导入和使用阿里云OSSsdk的方法
2017/03/15 PHP
PHP基于Closure类创建匿名函数的方法详解
2017/08/17 PHP
PHP实现的超长文本分页显示功能示例
2018/06/04 PHP
基于jQuery的弹出消息插件 DivAlert之旅(一)
2010/04/01 Javascript
JavaScrip单线程引擎工作原理分析
2010/09/04 Javascript
js动态创建上传表单通过iframe模拟Ajax实现无刷新
2014/02/20 Javascript
JS实现在线统计一个页面内鼠标点击次数的方法
2015/02/28 Javascript
点评js异步加载的4种方式
2015/12/22 Javascript
每日十条JavaScript经验技巧(一)
2016/06/23 Javascript
vue.js 使用axios实现下载功能的示例
2018/03/05 Javascript
react同构实践之实现自己的同构模板
2019/03/13 Javascript
使用nodejs分离html文件里的js和css详解
2019/04/12 NodeJs
javascript实现前端分页效果
2020/06/24 Javascript
[29:23]2014 DOTA2国际邀请赛中国区预选赛 LGD-GAMING VS CIS 第一场1
2014/05/23 DOTA
玩转python爬虫之爬取糗事百科段子
2016/02/17 Python
基于Django用户认证系统详解
2018/02/21 Python
python pandas 组内排序、单组排序、标号的实例
2018/04/12 Python
详解python算法之冒泡排序
2019/03/05 Python
python+OpenCV实现车牌号码识别
2019/11/08 Python
如何使用Python脚本实现文件拷贝
2019/11/20 Python
使用tensorflow实现VGG网络,训练mnist数据集方式
2020/05/26 Python
python在一个范围内取随机数的简单实例
2020/08/16 Python
css3 transform属性详解
2014/09/30 HTML / CSS
HTML5公共页面提取作为公用代码的方法
2020/06/30 HTML / CSS
python re模块和正则表达式
2021/03/24 Python
舞蹈专业求职信
2014/06/13 职场文书
四查四看自我剖析材料
2014/09/19 职场文书
运动会400米加油稿(8篇)
2014/09/22 职场文书
健康教育主题班会
2015/08/14 职场文书
新手必备Python开发环境搭建教程
2021/05/28 Python