方法一:直接调用
01 | <? |
02 | /******************************************************************************/ |
03 | /* 文件名 : soapclient.php |
04 | /* 说 明 : WebService接口客户端例程 |
05 | /******************************************************************************/ |
06 | include ( 'NuSoap.php' ); |
07 |
08 | // 创建一个soapclient对象,参数是server的WSDL |
09 | $client = new soapclient( 'http://localhost/Webservices/Service.asmx?WSDL' , 'wsdl' ); |
10 |
11 | // 参数转为数组形式传递 @camnpr |
12 | $aryPara = array ( 'strUsername' => 'username' , 'strPassword' =>MD5( 'password' )); |
13 |
14 | // 调用远程函数 |
15 | $aryResult = $client ->call( 'login' , $aryPara ); |
16 |
17 | //echo $client->debug_str; |
18 | /* |
19 | if (!$err=$client->getError()) { |
20 | print_r($aryResult); |
21 | } else { |
22 | print "ERROR: $err"; |
23 | } |
24 | */ |
25 |
26 | $document = $client ->document; |
27 | echo <<<SoapDocument |
28 | <?xml version= "1.0" encoding= "GB2312" ?> |
29 | <SOAP-ENV:Envelope SOAP-ENV:encodingStyle= "http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV= "http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd= "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" > |
30 | <SOAP-ENV:Body> |
31 | $document |
32 | </SOAP-ENV:Body> |
33 | </SOAP-ENV:Envelope> |
34 | SoapDocument; |
35 |
36 | ?> |
01 | <? |
02 | /******************************************************************************/ |
03 | /* 文件名 : soapclient.php |
04 | /* 说 明 : WebService接口客户端例程 |
05 | /******************************************************************************/ |
06 | include ( 'NuSoap.php' ); |
07 | // 创建一个soapclient对象,参数是server的WSDL @郑州网建 |
08 | $client = new soapclient( 'http://localhost/Webservices/Service.asmx?WSDL' , 'wsdl' ); |
09 | // 参数转为数组形式传递 |
10 | $aryPara = array ( 'strUsername' => 'username' , 'strPassword' =>MD5( 'password' )); |
11 | // 调用远程函数 |
12 | $aryResult = $client ->call( 'login' , $aryPara ); |
13 | //echo $client->debug_str; |
14 | /* |
15 | if (!$err=$client->getError()) { |
16 | print_r($aryResult); |
17 | } else { |
18 | print "ERROR: $err"; |
19 | } |
20 | */ |
21 | $document = $client ->document; |
22 | echo <<<SoapDocument |
23 | <?xml version= "1.0" encoding= "GB2312" ?> |
24 | <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" > |
25 | <SOAP-ENV:Body> |
26 | $document |
27 | </SOAP-ENV:Body> |
28 | </SOAP-ENV:Envelope> |
29 | SoapDocument; |
30 | ?> |
方法二:代理方式调用
01 | <? |
02 | /******************************************************************************/ |
03 | /* 文件名 : soapclient.php |
04 | /* 说 明 : WebService接口客户端例程 |
05 | /******************************************************************************/ |
06 | require ( 'NuSoap.php' ); |
07 |
08 | //创建一个soapclient对象,参数是server的WSDL @camnpr |
09 | $client = new soapclient( 'http://localhost/Webservices/Service.asmx?WSDL' , 'wsdl' ); |
10 |
11 | //生成proxy类 |
12 | $proxy = $client ->getProxy(); |
13 |
14 | //调用远程函数 |
15 | $aryResult = $proxy ->login( 'username' ,MD5( 'password' )); |
16 |
17 | //echo $client->debug_str; |
18 | /* |
19 | if (!$err=$proxy->getError()) { |
20 | print_r($aryResult); |
21 | } else { |
22 | print "ERROR: $err"; |
23 | } |
24 | */ |
25 |
26 | $document = $proxy ->document; |
27 | echo <<<SoapDocument |
28 | <?xml version= "1.0" encoding= "GB2312" ?> |
29 | <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" > |
30 | <SOAP-ENV:Body> |
31 | $document |
32 | </SOAP-ENV:Body> |
33 | </SOAP-ENV:Envelope> |
34 | SoapDocument; |
35 |
36 | ?> |
01 | <? |
02 | /******************************************************************************/ |
03 | /* 文件名 : soapclient.php |
04 | /* 说 明 : WebService接口客户端例程 |
05 | /******************************************************************************/ |
06 | require ( 'NuSoap.php' ); |
07 | //创建一个soapclient对象,参数是server的WSDL @郑州网建 |
08 | $client = new soapclient( 'http://localhost/Webservices/Service.asmx?WSDL' , 'wsdl' ); |
09 | //生成proxy类 |
10 | $proxy = $client ->getProxy(); |
11 | //调用远程函数 |
12 | $aryResult = $proxy ->login( 'username' ,MD5( 'password' )); |
13 | //echo $client->debug_str; |
14 | /* |
15 | if (!$err=$proxy->getError()) { |
16 | print_r($aryResult); |
17 | } else { |
18 | print "ERROR: $err"; |
19 | } |
20 | */ $document = $proxy ->document; |
21 | echo <<<SoapDocument |
22 | <?xml version= "1.0" encoding= "GB2312" ?> |
23 | <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" > |
24 | <SOAP-ENV:Body> |
25 | $document |
26 | </SOAP-ENV:Body> |
27 | </SOAP-ENV:Envelope> |
28 | SoapDocument; |
29 | ?> |
许多使用NuSoap 调用.NET WebService或J2EE WebService的朋友可能都遇到过中文乱码问题,下面介绍这一问题的出现的原因和相应的解决方法。
NuSoap调用WebService出现乱码的原因:
通常我们进行WebService开发时都是用的UTF-8编码,这时我们需要设置:
1 | $client ->soap_defencoding = 'utf-8' ; |
2 | $client ->soap_defencoding = 'utf-8' ; |
同时,需要让xml以同样的编码方式传递:
1 | $client ->xml_encoding = 'utf-8' ; |
2 | $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,因此,我们需要设置:
1 | $client ->soap_defencoding = 'utf-8' ; |
2 | $client ->decode_utf8 = false; |
3 | $client ->xml_encoding = 'utf-8' ; |
4 | $client ->soap_defencoding = 'utf-8' ; |
5 | $client ->decode_utf8 = false; |
6 | $client ->xml_encoding = 'utf-8' ; |