一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - PHP教程 - 淺談PHP調用Webservice思路及源碼分享

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

2020-07-01 14:24PHP教程網 PHP教程

NuSoap是PHP環境下的WebService編程工具,用于創建或調用WebService。它是一個開源軟件,是完全采用PHP語言編寫的、通過HTTP收發SOAP消息的一系列PHP類。NuSOAP的一個優勢是不需要擴展庫的支持,這種特性使得NuSoap可以用于所有的PHP環境,

方法一:直接調用

 

復制代碼 代碼如下:

<? 
/******************************************************************************/
/*  文件名 : 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 <<<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');

 

// 創建一個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 <<<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');  

//創建一個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 <<<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');

 

//創建一個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 <<<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 調用.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';

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 日本动漫黄网站在线观看 | 无人区在线观看免费观看 | 精品视频在线免费看 | 欧美日韩一二三区免费视频观看 | 我的年轻漂亮继坶三级 | 精品欧美一区二区三区久久久 | 青青青青青操 | 大象传媒2021秘密入口 | 武侠艳妇屈辱的张开双腿 | 亚洲欧美精品一区天堂久久 | 我的漂亮朋友在线观看全集免费 | 成人精品mv视频在线观看 | 美女靠逼免费网站 | 波多野结衣无码 | 亚洲精品第五页中文字幕 | 亚洲 日韩 国产 制服 在线 | 日韩一区在线播放 | 青青青国产精品国产精品久久久久 | 免费99精品国产自在现线 | 91香蕉国产 | 久久AV喷吹AV高潮欧美 | 欧美精品久久久亚洲 | 性做久久久久久久久浪潮 | 欧美日韩国产成人综合在线 | 亚洲精品国产国语 | 我半夜摸妺妺的奶C了她 | 国模大胆一区二区三区 | 国产精品原创巨作无遮挡 | 高清国产欧美一v精品 | 久久精品人人做人人爽97 | 99国内精品久久久久久久黑人 | 日本中年japanesebear | 韩国帅男同gay网站 韩国三级在线播放 | aaa在线| 亚洲日本va午夜中文字幕 | 欧美草逼网站 | 日本护士撒尿xxxx18 | 天堂漫画破解版 | 免费观看韩剧网站在线观看 | 国产综合成人亚洲区 | 男人天堂999|