浅谈PHP调用Webservice思路及源码分享
方法一:直接调用
<?
/******************************************************************************/
/* 文件名 : 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';