PHPsoap instance (2)

Source: Internet
Author: User
The implemented Soap server is provided to the client for calling functions, accept the parameters passed in by the client, and write
The implemented Soap server is provided to the client for calling the function, accept the parameters passed by the client, and
The processing result is returned to the client. The function functions as the sum of the returned parameters. the specific code for file name soapServer. php is as follows:
  /**
* @ Soap instance 2 Server example
* @ Author PHP technology blog, PHP blog, WWW. PHPCQ. COM
*/
Function soap_add ($ num1, $ num2 ){
If (trim ($ num1 )! = (Int) $ num2 | trim ($ num2 )! = (Int) $ num2 ){
Return new SoapFault ('1', 'invalid user data! ');
} Else {
Return $ num1 + $ num2;
}
}
$ SoapServer = new SoapServer (null, array ('URL' => 'Soap '); // Create a SoapServer object
$ SoapServer-> addFunction ('soap _ add'); // register a service function
$ SoapServer-> handle (); // process and generate Soap
The above code compiled a soap_add function to implement the sum of two parameters, and the page supports remote calls from the client.

Client compilation
/**
* @ Soap example 2 Client
* @ Author PHP technology blog, PHP blog, WWW. PHPCQ. COM
*/
Try {

// Create a client instance
$ SoapClient = new SoapClient (null,
Array ('location' => 'http: // www.phpcq.com/soapServer.php ',
'URL' => 'Soap '));
$ Num1 = 1; $ num2 = 2;
$ Sum = $ soapClient-> soap_add ($ num1, $ num2 );
Echo $ num1 + $ num2 = $ sum;
} Catch (SoapFault $ fault ){
Echo "error [$ fault-> faultcode: $ fault-> faultstring]";
}
The client code uses the try... catch () statement to implement possible errors during Soap access to the server.
Output in the appropriate format.

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.