PHP Webservice call ideas and source code sharing _ PHP Tutorial

Source: Internet
Author: User
Let's talk about how PHP calls Webservice and its source code. Method 1: directly call and copy the code as follows :? **************************************** * ************************************ File name: soapclient. php * Description: We Method 1: Direct call

The code is as follows:


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

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

// Input parameters in array format
$ AryPara = array ('strusername' => 'username', 'strpassword' => MD5 ('password '));

// Call a 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;

?>

The code is as follows:


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

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

// Input parameters in array format
$ AryPara = array ('strusername' => 'username', 'strpassword' => MD5 ('password '));

// Call a 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

The code is as follows:


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

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

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

// Call a 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;

?>

The code is as follows:


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

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

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

// Call a 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 Chinese garbled characters. The following describes the causes and solutions of this problem.
Reasons for garbled WebService calling by NuSoap:
Usually we do WebService development is used for UTF-8 encoding, then we need to set:

The code is as follows:


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

At the same time, xml must be transmitted in the same encoding mode:

The code is as follows:


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


At this point, everything is normal, but when we output the results, we find that the returned code is garbled.
Solution to WebService call garbled by NuSoap:
In fact, a friend who has enabled the debugging function will find that $ client-> response returns the correct result. why $ result = $ client-> call ($ action, array ('parameters '=> $ param); is it garbled?
After studying the NuSoap code, we will find that when xml_encoding is set to UTF-8, NuSoap will detect the decode_utf8 setting. if it is true, it will execute the utf8_decode function in PHP, nuSoap is set to true by default. Therefore, we need to set:

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

The pipeline code is as follows :? /*************************************** ***************************************/ /* File name: soapclient. php/* Description: We...

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.