浅谈PHP调用Webservice思路及源码分享

前端技术 2023/09/03 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;

?>

本文地址:https://www.stayed.cn/item/7835

转载请注明出处。

本站部分内容来源于网络,如侵犯到您的权益,请 联系我

我的博客

人生若只如初见,何事秋风悲画扇。