PHP调用Webservice实例
方法一:直接調(diào)用
?2?/******************************************************************************/
?3?/*??文件名?:?soapclient.php
?4?/*??說??明?:?WebService接口客戶端例程
?5?/******************************************************************************/
?6?include('NuSoap.php');
?7?//?創(chuàng)建一個(gè)soapclient對(duì)象,參數(shù)是server的WSDL?
?8?$client?=?new?soapclient('http://localhost/Webservices/Service.asmx?WSDL',?'wsdl');
?9?//?參數(shù)轉(zhuǎn)為數(shù)組形式傳遞
10?$aryPara?=?array('strUsername'=>'username',?'strPassword'=>MD5('password'));
11?//?調(diào)用遠(yuǎn)程函數(shù)
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??>
?
方法二:代理方式調(diào)用
?2?/******************************************************************************/
?3?/*??文件名?:?soapclient.php
?4?/*??說??明?:?WebService接口客戶端例程
?5?/******************************************************************************/
?6?require('NuSoap.php');?
?7?//創(chuàng)建一個(gè)soapclient對(duì)象,參數(shù)是server的WSDL?
?8?$client=new?soapclient('http://localhost/Webservices/Service.asmx?WSDL',?'wsdl');?
?9?//生成proxy類?
10?$proxy=$client->getProxy();?
11?//調(diào)用遠(yuǎn)程函數(shù)?
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?*/
21?$document=$proxy->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??>
許多使用NuSoap 調(diào)用.NET WebService或J2EE??WebService的朋友可能都遇到過中文亂碼問題,下面介紹這一問題的出現(xiàn)的原因和相應(yīng)的解決方法。?
NuSoap調(diào)用WebService出現(xiàn)亂碼的原因:
通常我們進(jìn)行WebService開發(fā)時(shí)都是用的UTF-8編碼,這時(shí)我們需要設(shè)置:
同時(shí),需要讓xml以同樣的編碼方式傳遞:
至此應(yīng)該是一切正常了才對(duì),但是我們?cè)谳敵鼋Y(jié)果的時(shí)候,卻發(fā)現(xiàn)返回的是亂碼。
NuSoap調(diào)用WebService出現(xiàn)亂碼的解決方法:
實(shí)際上,開啟了調(diào)試功能的朋友,相信會(huì)發(fā)現(xiàn)$client->response返回的是正確的結(jié)果,為什么$result = $client->call($action, array('parameters' => $param)); 卻是亂碼呢?
研究過NuSoap代碼后我們會(huì)發(fā)現(xiàn),當(dāng)xml_encoding設(shè)置為UTF-8時(shí),NuSoap會(huì)檢測(cè)decode_utf8的設(shè)置,如果為true,會(huì)執(zhí)行 PHP 里面的utf8_decode函數(shù),而NuSoap默認(rèn)為true,因此,我們需要設(shè)置:
$client->decode_utf8?=?false;
$client->xml_encoding?=?'utf-8';
轉(zhuǎn)載于:https://www.cnblogs.com/xieyunc/archive/2009/05/01/1447450.html
總結(jié)
以上是生活随笔為你收集整理的PHP调用Webservice实例的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Singleton模式学习
- 下一篇: JSON asp(vbs)中文支持问题