淺談PHP調用Webservice思路及源碼分享_PHP教程

來源:互聯網
上載者:User
方法一:直接調用

複製代碼 代碼如下:
/******************************************************************************/
/* 檔案名稱 : 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 <<



$document


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 <<



$document


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 <<



$document


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 <<



$document


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';

  至此應該是一切正常了才對,但是我們在輸出結果的時候,卻發現返回的是亂碼。
  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->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';

http://www.bkjia.com/PHPjc/779568.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/779568.htmlTechArticle方法一:直接調用 複製代碼 代碼如下: ? /******************************************************************************/ /* 檔案名稱 : soapclient.php /* 說 明 : We...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.