Nusoap Introduction PHP using WebService

Source: Internet
Author: User
Tags http post php language soap php soap server wsdl

Many organizations have adopted Apach and PHP as their Web application environment. The use of PHP in Web Services mode may seem more difficult. But in fact, with Nusoap, you can easily apply PHP to build soap's client and server side.  

 
PHP soap server  
  It is easy to build a SOAP server with PHP and Nusoap. Basically, you just write out the functions you want to expose to your Web services, and then register them with Nusoap. OK, another two steps will be required to complete the creation of the PHP soap server. First you have to create an instance of the Nusoap object in your PHP code, and then use the HTTP POST method to pass the raw data to nusoap for processing  
Nusoap is easier to use, the most commonly used class is SOAP_ Server and SoapClient, where soap_server is used to create the WebService service, and class soapclient is used to invoke webservice . These two classes are defined in lib/nusoap.php, So we need to reference the file when we create or invoke the WebService interface program . 

 
Nusoap is a WebService programming tool in a PHP environment for creating or invoking webservice. It is an open source software, a series of PHP classes that are written in PHP language and send and receive SOAP messages over HTTP, by Nuspherecorporation (http://dietrich.ganx4.com/nusoap/   ) development. One advantage of Nusoap is that there is no need for extended library support, which allows Nusoap to be used in all PHP environments and is not affected by server security settings.   
 1.  First, go to http://sourceforge.net/projects/nusoap/   download Nusoap-0.9.5.zip, unzip the The Lib folder is placed in the same directory as your WebService program, such as/webservice/lib.
  2.  Server: Create a nusoapservice.php file.    

<?PHPrequire_once("lib/nusoap.php");$server=Newsoap_server ();//Avoid garbled characters$server->soap_defencoding = ' UTF-8 ';$server->decode_utf8 =false;$server->xml_encoding = ' UTF-8 ';$server-&GT;CONFIGUREWSDL (' SayHello ');//Open WSDL Support/*register the corresponding value of the program type that needs to be accessed by the client: bool-> "Xsd:boolean";
String-> "Xsd:string"; Int-> "Xsd:int";
Float-> "Xsd:float";*/$server->register (' SayHello ',//Method Name              Array("Name" = "xsd:string"),//parameter, the default is "xsd:string";              Array("Return" = "xsd:string")// return value, default to "Xsd:string";
);//isset Detect if the variable is set$HTTP _raw_post_data=isset($HTTP _raw_post_data) ?$HTTP _raw_post_data: ‘‘;//Service processing the data entered by the client$server->service ($HTTP _raw_post_data );/** * method to invoke * @param $name*/functionSayHello ($name) { return"Hello, {$name} !";}?>

3. Client: Establish the nusoapclient.php file.

<?PHPrequire_once("lib/nusoap.php");/*the address of the WSDL file called by the wsdl WebService parameter 1 (the WSDL after the question mark cannot be uppercase) parameter 2 specifies whether to use WSDL $client = n        EW soapclient (' http://localhost/WebService/nusoapService.php?wsdl ', true); */$client=NewSoapClient (' http://localhost/WebService/nusoapService.php ' );$client->soap_defencoding = ' UTF-8 ';$client->decode_utf8 =false;$client->xml_encoding = ' UTF-8 ';//parameter to array form pass$paras=Array(' Name ' =>; ' Bruce Lee ' );//The following arguments can be omitted when the target method has no arguments$result=$client->call (' SayHello ',$paras );//Check for errors, get return valuesif(!$err=$client-GetError ()) {    Echo"Return Result:",$result;} Else {    Echo"Call Error:",$err;}?>

Note:
Implement WebService with Nusoap, do not open PHP SOAP extensions

The file code cannot have any output, otherwise the call will report an error similar to the following:
XML error parsing SOAP payload on Linex (line number): Reserved XML Name

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.