PHP调用Webservice实例
生活随笔
收集整理的這篇文章主要介紹了
PHP调用Webservice实例
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
NuSoap是PHP環境下的WebService編程工具,用于創建或調用WebService。它是一個開源軟件,是完全采用PHP語言編寫 的、通過HTTP收發SOAP消息的一系列PHP類,由NuSphere Corporation(http://dietrich.ganx4.com/nusoap/ )開發。NuSOAP的一個優勢是不需要擴展庫的支持,這種特性使得NuSoap可以用于所有的PHP環境,不受服務器安全設置的影響。
?2?/******************************************************************************/
?3?/*??文件名?:?soapclient.php
?4?/*??說??明?:?WebService接口客戶端例程
?5?/******************************************************************************/
?6?include('NuSoap.php');
?7?//?創建一個soapclient對象,參數是server的WSDL?
?8?$client?=?new?soapclient('http://localhost/Webservices/Service.asmx?WSDL',?'wsdl');
?9?//?參數轉為數組形式傳遞
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??>
?2?/******************************************************************************/
?3?/*??文件名?:?soapclient.php
?4?/*??說??明?:?WebService接口客戶端例程
?5?/******************************************************************************/
?6?require('NuSoap.php');?
?7?//創建一個soapclient對象,參數是server的WSDL?
?8?$client=new?soapclient('http://localhost/Webservices/Service.asmx?WSDL',?'wsdl');?
?9?//生成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?*/
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??>
$client->decode_utf8?=?false;
$client->xml_encoding?=?'utf-8';
方法一:直接調用
?2?/******************************************************************************/
?3?/*??文件名?:?soapclient.php
?4?/*??說??明?:?WebService接口客戶端例程
?5?/******************************************************************************/
?6?include('NuSoap.php');
?7?//?創建一個soapclient對象,參數是server的WSDL?
?8?$client?=?new?soapclient('http://localhost/Webservices/Service.asmx?WSDL',?'wsdl');
?9?//?參數轉為數組形式傳遞
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??>
?
方法二:代理方式調用
?2?/******************************************************************************/
?3?/*??文件名?:?soapclient.php
?4?/*??說??明?:?WebService接口客戶端例程
?5?/******************************************************************************/
?6?require('NuSoap.php');?
?7?//創建一個soapclient對象,參數是server的WSDL?
?8?$client=new?soapclient('http://localhost/Webservices/Service.asmx?WSDL',?'wsdl');?
?9?//生成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?*/
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 調用.NET WebService或J2EE??WebService的朋友可能都遇到過中文亂碼問題,下面介紹這一問題的出現的原因和相應的解決方法。?
NuSoap調用WebService出現亂碼的原因:
通常我們進行WebService開發時都是用的UTF-8編碼,這時我們需要設置:
同時,需要讓xml以同樣的編碼方式傳遞:
至此應該是一切正常了才對,但是我們在輸出結果的時候,卻發現返回的是亂碼。
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->decode_utf8?=?false;
$client->xml_encoding?=?'utf-8';
轉載于:https://www.cnblogs.com/xieyunc/archive/2009/05/01/1447450.html
總結
以上是生活随笔為你收集整理的PHP调用Webservice实例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Singleton模式学习
- 下一篇: 标 题: 腾讯面试题目(PHP程序员)