方法一:直接调用
<? /******************************************************************************/ /* 文件名 : 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';
$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';