浅谈PHP调用Webservice思路及源码分享
php  /  管理员 发布于 7年前   328
方法一:直接调用 // 创建一个soapclient对象,参数是server的WSDL // 参数转为数组形式传递 // 调用远程函数 //echo $client->debug_str; $document=$client->document; ?> // 创建一个soapclient对象,参数是server的WSDL // 参数转为数组形式传递 // 调用远程函数 //echo $client->debug_str; $document=$client->document; ?> 方法二:代理方式调用 //创建一个soapclient对象,参数是server的WSDL //生成proxy类 //调用远程函数 //echo $client->debug_str; $document=$proxy->document; ?> //创建一个soapclient对象,参数是server的WSDL //生成proxy类 //调用远程函数 //echo $client->debug_str; $document=$proxy->document; 许多使用NuSoap 调用.NET WebService或J2EE WebService的朋友可能都遇到过中文乱码问题,下面介绍这一问题的出现的原因和相应的解决方法。 同时,需要让xml以同样的编码方式传递: 122 在 123 在 原梓番博客 在 博主 在 1111 在
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号
/******************************************************************************/
/* 文件名 : soapclient.php
/* 说 明 : WebService接口客户端例程
/******************************************************************************/
include('NuSoap.php');
$client = new soapclient('http://localhost/Webservices/Service.asmx?WSDL', 'wsdl');
$aryPara = array('strUsername'=>'username', 'strPassword'=>MD5('password'));
$aryResult = $client->call('login',$aryPara);
/*
if (!$err=$client->getError()) {
print_r($aryResult);
} else {
print "ERROR: $err";
}
*/
echo <<
$document
SoapDocument;
/******************************************************************************/
/* 文件名 : soapclient.php
/* 说 明 : WebService接口客户端例程
/******************************************************************************/
include('NuSoap.php');
$client = new soapclient('http://localhost/Webservices/Service.asmx?WSDL', 'wsdl');
$aryPara = array('strUsername'=>'username', 'strPassword'=>MD5('password'));
$aryResult = $client->call('login',$aryPara);
/*
if (!$err=$client->getError()) {
print_r($aryResult);
} else {
print "ERROR: $err";
}
*/
echo <<
$document
SoapDocument;
/******************************************************************************/
/* 文件名 : soapclient.php
/* 说 明 : WebService接口客户端例程
/******************************************************************************/
require('NuSoap.php');
$client=new soapclient('http://localhost/Webservices/Service.asmx?WSDL', 'wsdl');
$proxy=$client->getProxy();
$aryResult=$proxy->login('username',MD5('password'));
/*
if (!$err=$proxy->getError()) {
print_r($aryResult);
} else {
print "ERROR: $err";
}
*/
echo <<
$document
SoapDocument;
/******************************************************************************/
/* 文件名 : soapclient.php
/* 说 明 : WebService接口客户端例程
/******************************************************************************/
require('NuSoap.php');
$client=new soapclient('http://localhost/Webservices/Service.asmx?WSDL', 'wsdl');
$proxy=$client->getProxy();
$aryResult=$proxy->login('username',MD5('password'));
/*
if (!$err=$proxy->getError()) {
print_r($aryResult);
} else {
print "ERROR: $err";
}
*/
echo <<
$document
SoapDocument;
?>
NuSoap调用WebService出现乱码的原因:
通常我们进行WebService开发时都是用的UTF-8编码,这时我们需要设置:
$client->soap_defencoding = 'utf-8';
$client->soap_defencoding = 'utf-8';
$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'; 您可能感兴趣的文章: