PHP调用Webservice实例[通俗易懂]

PHP调用Webservice实例[通俗易懂]NuSoap是PHP环境下的WebService编程工具,用于创建或调用WebService。它是一个开源软件,是完全采用PHP语言编写的、通过HTTP收发SOAP消息的一系列PHP类,由NuSphereCorporation(http://dietrich.ganx4.com/nusoap/)开发。NuSOAP的一个优势是不需要扩展库的支持,这种特性使得NuSoap可以用于所有的PHP环境,

大家好,又见面了,我是你们的朋友全栈君。

NuSoap是PHP环境下的WebService编程工具,用于创建或调用WebService。它是一个开源软件,是完全采用PHP语言编写的、通过HTTP收发SOAP消息的一系列PHP类,由NuSphere Corporation(http://dietrich.ganx4.com/nusoap/ )开发。NuSOAP的一个优势是不需要扩展库的支持,这种特性使得NuSoap可以用于所有的PHP环境,不受服务器安全设置的影响。  

方法一:直接调用
view plaincopy to clipboardprint?
<?  
/******************************************************************************/ 
/*  文件名 : 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;

?>
方法二:代理方式调用
view plaincopy to clipboardprint?
<?  
/******************************************************************************/ 
/*  文件名 : 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编码,这时我们需要设置:
view plaincopy to clipboardprint?
$client->soap_defencoding = ‘utf-8’; 
$client->soap_defencoding = ‘utf-8’;

  同时,需要让xml以同样的编码方式传递:
view plaincopy to clipboardprint?
$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,因此,我们需要设置:
view plaincopy to clipboardprint?
$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’;

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/162642.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

  • Open-E DSS V7 应用系列之七 卷组和卷的管理

    Open-E DSS V7 应用系列之七 卷组和卷的管理

  • ubuntu top参数_top命令详解

    ubuntu top参数_top命令详解Linux真是太强大了。查看Ubuntu的资源占用的命令为$:toptop命令就可以查看内存,cpu和进程了,很方便top:主要参数d:指定更新的间隔,以秒计算。q:没有任何延迟的更新。如果使用者有超级用户,则top命令将会以最高的优先序执行。c:显示进程完整的路径与名称。S:累积模式,会将己完成或消失的子行程的CPU时间累积起来。s:安全模式。i:不显示任何闲置…

  • CentOS7修改IP地址后无法ping通问题记录[通俗易懂]

    CentOS7修改IP地址后无法ping通问题记录在键入ifconfigenp0s3192.168.56.2后,在cmd中无法ping通此IP地址解决办法:键入sudoifconfigenp0s3192.168.56.2

  • Quartus II 13.1的安装及使用

    Quartus II 13.1的安装及使用QuartusII的安装及使用前言一、QuartusII的下载二、QuartusII的安装三、QuartusII的注册四、QuartusII的使用(一)相关驱动的配置(二)使用流程的认识(三)使用过程总结前言本文章是对QuartusII13.1的安装及使用方法的介绍说明。一、QuartusII的下载百度网盘下载链接:https://pan.baidu.com/s/1a9d-bq9RZmWrRV542X4IEA提取码:ifte说明:本链接来自于正点原子官方资料下载二、

    2022年10月16日
  • .net cms开源_基于vue的开源CMS

    .net cms开源_基于vue的开源CMS提起开源cms,大家第一想到的是php的cms,因为php开源的最早,也最为用户和站长们认可,随着各大cms系统的功能的不断完善和各式各样的开源cms的出现,.net和java的高端的cms系统也逐渐的走上了开源的路线,尤其是.net的cms系统,从最早国外的开源,到现在国内致力于.net的cms系统的研发的公司和团队也渐渐认清楚开源路线的必然性,于是乎竞相的提出开源战略路线,但有的还是并不是全

  • 个人博客数据库设计

    个人博客数据库设计文章目录前言一、概述及分析1.1项目背景1.2分析1.3系统功能1.3.1用户管理1.3.2博文管理1.3.3评论管理1.3.4分类管理1.3.5标签管理二、数据库概念模型设计—基本ER图三、数据库逻辑模型设计四、数据库语句4.1建立数据表语句(部分)4.2建立视图(部分)五、小结&amp;参考资料小结参考资料前言最近要做数据库大作业,在思考了很久之后,还是设计一个简单…

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号