Home  >  Article  >  Backend Development  >  A brief discussion on PHP calling Webservice ideas and source code sharing_PHP tutorial

A brief discussion on PHP calling Webservice ideas and source code sharing_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:28:59652browse

Method 1: Call directly

Copy code The code is as follows:

/******************************************************************************/
/ * File name: soapclient.php
/* Description: WebService interface client routine
/******************************************************************************/
include('NuSoap.php');

//Create a soapclient object, the parameter is the server's WSDL
$client = new soapclient('http://localhost/Webservices/Service.asmx?WSDL', 'wsdl');

// The parameters are transferred in array form
$aryPara = array('strUsername'=>'username', 'strPassword'=>MD5('password'));

// Call remote function
$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;

?>

Copy code The code is as follows:

/******************************************************************************/
/ * File name: soapclient.php
/* Description: WebService interface client routine
/******************************************************************************/
include('NuSoap.php');

// Create a soapclient object, the parameter is the WSDL of the server
$client = new soapclient('http://localhost/Webservices/Service.asmx?WSDL', 'wsdl');

//Parameters are transferred in array form
$aryPara = array('strUsername'=>'username', 'strPassword'=>MD5('password'));

// Call remote function
$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;

?>

Method 2: Proxy call

Copy code The code is as follows:

/******************************************************************************/
/* File name: soapclient.php
/* Description: WebService interface client routine
/******************************************************************************/
require('NuSoap.php');

//Create a soapclient object, the parameter is the WSDL of the server
$client=new soapclient('http:// localhost/Webservices/Service.asmx?WSDL', 'wsdl');

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

// Call remote function
$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;

?>

Copy code The code is as follows:

/******************************************************************************/
/ * File name: soapclient.php
/* Description: WebService interface client routine
/******************************************************************************/
require('NuSoap.php');

//Create a soapclient object, the parameter is the WSDL of the server
$client=new soapclient('http://localhost/Webservices/Service.asmx?WSDL', 'wsdl');

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

//Call remote function
$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;
?>

Many friends who use NuSoap to call .NET WebService or J2EE WebService may have encountered the problem of Chinese garbled characters. The following introduces the reasons for this problem and the corresponding solutions.
The reason for garbled characters when NuSoap calls WebService:
Usually when we develop WebService, we use UTF-8 encoding. At this time, we need to set:

Copy code The code is as follows:

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

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

Copy the code The code is as follows:

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

Everything should be normal at this point, but when we output the results, we found that the returned code was garbled.
Solution to garbled code when NuSoap calls WebService:
In fact, friends who have turned on the debugging function will find that $client->response returns the correct result, why $result = $client-> call($action, array('parameters' => $param)); is garbled?
After studying the NuSoap code, we will find that when xml_encoding is set to UTF-8, NuSoap will detect the setting of decode_utf8. If it is true, the utf8_decode function in PHP will be executed, and NuSoap defaults to true, so we need Settings:
Copy code The code is as follows:

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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/779568.htmlTechArticleMethod 1: Directly call the copied code. The code is as follows: ? /******************************************************************************/ /* File name: soapclient.php /* Description: We...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn