淺談PHP調(diào)用Webservice思路及源碼分享
方法一:直接調(diào)用
<?
/******************************************************************************/
/* 文件名 : soapclient.php
/* 說 明 : WebService接口客戶端例程
/******************************************************************************/
include('NuSoap.php');
// 創(chuàng)建一個soapclient對象,參數(shù)是server的WSDL
$client = new soapclient('http://localhost/Webservices/Service.asmx?WSDL', 'wsdl');
// 參數(shù)轉(zhuǎn)為數(shù)組形式傳遞
$aryPara = array('strUsername'=>'username', 'strPassword'=>MD5('password'));
// 調(diào)用遠(yuǎn)程函數(shù)
$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');
// 創(chuàng)建一個soapclient對象,參數(shù)是server的WSDL
$client = new soapclient('http://localhost/Webservices/Service.asmx?WSDL', 'wsdl');
// 參數(shù)轉(zhuǎn)為數(shù)組形式傳遞
$aryPara = array('strUsername'=>'username', 'strPassword'=>MD5('password'));
// 調(diào)用遠(yuǎn)程函數(shù)
$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;
?>
方法二:代理方式調(diào)用
<?
/******************************************************************************/
/* 文件名 : soapclient.php
/* 說 明 : WebService接口客戶端例程
/******************************************************************************/
require('NuSoap.php');
//創(chuàng)建一個soapclient對象,參數(shù)是server的WSDL
$client=new soapclient('http://localhost/Webservices/Service.asmx?WSDL', 'wsdl');
//生成proxy類
$proxy=$client->getProxy();
//調(diào)用遠(yuǎn)程函數(shù)
$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');
//創(chuàng)建一個soapclient對象,參數(shù)是server的WSDL
$client=new soapclient('http://localhost/Webservices/Service.asmx?WSDL', 'wsdl');
//生成proxy類
$proxy=$client->getProxy();
//調(diào)用遠(yuǎn)程函數(shù)
$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 調(diào)用.NET WebService或J2EE WebService的朋友可能都遇到過中文亂碼問題,下面介紹這一問題的出現(xiàn)的原因和相應(yīng)的解決方法。
NuSoap調(diào)用WebService出現(xiàn)亂碼的原因:
通常我們進(jìn)行WebService開發(fā)時都是用的UTF-8編碼,這時我們需要設(shè)置:
$client->soap_defencoding = 'utf-8';
$client->soap_defencoding = 'utf-8';
同時,需要讓xml以同樣的編碼方式傳遞:
$client->xml_encoding = 'utf-8';
$client->xml_encoding = 'utf-8';
至此應(yīng)該是一切正常了才對,但是我們在輸出結(jié)果的時候,卻發(fā)現(xiàn)返回的是亂碼。
NuSoap調(diào)用WebService出現(xiàn)亂碼的解決方法:
實際上,開啟了調(diào)試功能的朋友,相信會發(fā)現(xiàn)$client->response返回的是正確的結(jié)果,為什么$result = $client->call($action, array('parameters' => $param)); 卻是亂碼呢?
研究過NuSoap代碼后我們會發(fā)現(xiàn),當(dāng)xml_encoding設(shè)置為UTF-8時,NuSoap會檢測decode_utf8的設(shè)置,如果為true,會執(zhí)行 PHP 里面的utf8_decode函數(shù),而NuSoap默認(rèn)為true,因此,我們需要設(shè)置:
$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';
- php5 apache 2.2 webservice 創(chuàng)建與配置(java)
- PHP中調(diào)用ASP.NET的WebService的代碼
- PHP調(diào)用Webservice實例代碼
- 在PHP中利用wsdl創(chuàng)建標(biāo)準(zhǔn)webservice的實現(xiàn)代碼
- php如何調(diào)用webservice應(yīng)用介紹
- PHP中如何調(diào)用webservice的實例參考
- PHP使用SOAP調(diào)用.net的WebService數(shù)據(jù)
- php的webservice的wsdl的XML無法顯示問題的解決方法
- PHP調(diào)用JAVA的WebService簡單實例
- php中創(chuàng)建和調(diào)用webservice接口示例
- php實現(xiàn)webservice實例
- 四種php中webservice實現(xiàn)的簡單架構(gòu)方法及實例
相關(guān)文章
thinkphp3.2嵌入百度編輯器ueditor的實例代碼
本篇文章主要介紹了thinkphp3.2嵌入百度編輯器ueditor的實例代碼,具有一定的參考價值,有興趣的可以了解一下2017-07-07discuz 首頁四格:最新話題+最新回復(fù)+熱門話題+精華文章插件
discuz 首頁四格:最新話題+最新回復(fù)+熱門話題+精華文章插件...2007-08-08Yii框架實現(xiàn)多數(shù)據(jù)庫配置和操作的方法
這篇文章主要介紹了Yii框架實現(xiàn)多數(shù)據(jù)庫配置和操作的方法,結(jié)合實例形式分析了Yii框架多數(shù)據(jù)庫的配置技巧與相關(guān)操作注意事項,需要的朋友可以參考下2017-05-05支付寶支付開發(fā)——當(dāng)面付條碼支付和掃碼支付實例
這篇文章主要介紹了支付寶支付開發(fā)——當(dāng)面付條碼支付和掃碼支付實例,具有一定的參考價值,有需要的可以了解一下。2016-11-11徹底刪除thinkphp3.1案例blog標(biāo)簽的方法
這篇文章主要介紹了徹底刪除thinkphp3.1案例blog標(biāo)簽的方法,以thinkphp3.1框架中的案例blog為基礎(chǔ)介紹了刪除日記時同步刪除tag冗余數(shù)據(jù)的方法,非常具有實用價值,需要的朋友可以參考下2014-12-12Laravel?Many-To-Many多對多關(guān)系模式示例詳解
這篇文章主要為大家介紹了Laravel?Many-To-Many多對多關(guān)系模式示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06Laravel 自動生成驗證的實例講解:login / logout
今天小編就為大家分享一篇Laravel 自動生成驗證的實例分析:login / logout,具有好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-10-10PHP 設(shè)計模式系列之 specification規(guī)格模式
規(guī)格模式是組合模式的一種擴(kuò)展,在框架性開發(fā)中使用較多(項目級開發(fā)很少使用),通過本文給大家介紹PHP 設(shè)計模式系列之 specification規(guī)格模式,對specification模式相關(guān)知識感興趣的朋友一起學(xué)習(xí)吧2016-01-01