Talking about the WebService idea of PHP calling and _php instance of source sharing

Source: Internet
Author: User
Tags soap wsdl

Method One: Direct call

Copy Code code as follows:

?
/******************************************************************************/
/* FileName: soapclient.php
/* Description: WebService interface Client Routines
/******************************************************************************/
Include (' nusoap.php ');

Creates a SoapClient object, which is the WSDL of the server
$client = new SoapClient (' Http://localhost/Webservices/Service.asmx?WSDL ', ' WSDL ');

parameter to the array form
$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;

?>

Copy Code code as follows:

?
/******************************************************************************/
/* FileName: soapclient.php
/* Description: WebService interface Client Routines
/******************************************************************************/
Include (' nusoap.php ');

Creates a SoapClient object, which is the WSDL of the server
$client = new SoapClient (' Http://localhost/Webservices/Service.asmx?WSDL ', ' WSDL ');

parameter to the array form
$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 Two: Proxy method call

Copy Code code as follows:

?
/******************************************************************************/
/* FileName: soapclient.php
/* Description: WebService interface Client Routines
/******************************************************************************/
Require (' nusoap.php ');

Creates a SoapClient object, which is the WSDL of the server
$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;

?>

Copy Code code as follows:

?
/******************************************************************************/
/* FileName: soapclient.php
/* Description: WebService interface Client Routines
/******************************************************************************/
Require (' nusoap.php ');

Creates a SoapClient object, which is the WSDL of the server
$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 invoke. NET WebService or Java EE WebService may have encountered Chinese garbled problems, the following describes the causes of the problem and the corresponding solutions.
Nusoap Call WebService the reason for garbled:
Usually we use the UTF-8 code for WebService development, when we need to set:

Copy Code code as follows:

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

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

Copy Code code as follows:

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

At this point should be all normal, but we are in the output of the results, but found that the return of garbled.
Nusoap Call WebService a garbled solution:
In fact, a friend who has turned on the debugging feature will find that $client->response is returning the correct result, why $result = $client->call ($action, Array (' Parameters ' => $ param)); But it's garbled?
After studying the NUSOAP code, we find that when xml_encoding is set to UTF-8, Nusoap detects Decode_utf8 settings, and if true, executes the Utf8_decode function in PHP, and Nusoap defaults to True , so we need to set:
Copy Code code 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 ';

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.