Talking about PHP Calling Webservice Thought and Source Sharing

  • 2021-06-28 12:10:01
  • OfStack

Method 1: Direct call


<?  
/******************************************************************************/ 
/*   file name  : soapclient.php 
/*   say    bright  : WebService Interface client routines  
/******************************************************************************/ 
include('NuSoap.php');  

//  Establish 1 individual soapclient Object, parameter is server Of WSDL   
$client = new soapclient('http://localhost/Webservices/Service.asmx?WSDL', 'wsdl');  

//  Parameters are passed as arrays   
$aryPara = array('strUsername'=>'username', 'strPassword'=>MD5('password'));  

//  Calling remote functions   
$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;  

?>  


<?
/******************************************************************************/
/*   file name  : soapclient.php
/*   say    bright  : WebService Interface client routines 
/******************************************************************************/
include('NuSoap.php');
//  Establish 1 individual soapclient Object, parameter is server Of WSDL
$client = new soapclient('http://localhost/Webservices/Service.asmx?WSDL', 'wsdl');
//  Parameters are passed as arrays 
$aryPara = array('strUsername'=>'username', 'strPassword'=>MD5('password'));
//  Calling remote functions 
$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;
?>

Method 2: Proxy call


<?  
/******************************************************************************/ 
/*   file name  : soapclient.php 
/*   say    bright  : WebService Interface client routines  
/******************************************************************************/ 
require('NuSoap.php');   

// Establish 1 individual soapclient Object, parameter is server Of WSDL   
$client=new soapclient('http://localhost/Webservices/Service.asmx?WSDL', 'wsdl');   

// generate proxy class    
$proxy=$client->getProxy();   

// Calling remote functions    
$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;  

?>  


<?
/******************************************************************************/
/*   file name  : soapclient.php
/*   say    bright  : WebService Interface client routines 
/******************************************************************************/
require('NuSoap.php');
// Establish 1 individual soapclient Object, parameter is server Of WSDL
$client=new soapclient('http://localhost/Webservices/Service.asmx?WSDL', 'wsdl');
// generate proxy class 
$proxy=$client->getProxy();
// Calling remote functions 
$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;
?>

Many friends who use NuSoap to call.NET WebService or J2EE WebService may have encountered Chinese scrambling problems. The reasons for this problem and the corresponding solutions are described below.
The reason why NuSoap calls WebService is garbled:
The UTF-8 code is usually used when we develop WebService, so we need to set it up:


$client->soap_defencoding = 'utf-8'; 
$client->soap_defencoding = 'utf-8';

At the same time, xml needs to be passed in the same encoding:


$client->xml_encoding = 'utf-8'; 
$client->xml_encoding = 'utf-8';

This is supposed to be all right until now, but when we output the results, we found that the return was garbled.
Solution for NuSoap calling WebService encoding:
In fact, a friend with debugging turned on is confident to find $client- > response returned the correct result, why $result = $client- > call($action, array('parameters' = > $param);But what about garbage?
After studying the NuSoap code, we will find that when xml_When encoding is set to UTF-8, NuSoap detects decode_utf8 setting, if true, executes utf8_inside PHPThe decode function, while NuSoap defaults to true, so we need to set:

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


Related articles: