Webservice--nusoap detailed

Source: Internet
Author: User
PHP SOAP Server

It is very 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 also 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

The use of Nusoap is relatively simple, with the most commonly used classes being soap_server and SoapClient, where Soap_server is used to create the WebService service, and class soapclient is used to invoke WebService The definitions of these two classes are all 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 for creating or invoking webservice in a PHP environment. It is an open source software, developed by Nusphere Corporation (http://dietrich.ganx4.com/nusoap/), a series of PHP classes written entirely in PHP that send and receive SOAP messages over HTTP. 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/ to download nusoap.zip.
2. Server : Establish nusoapservice.php file.

[Php]view plaincopy

  1. require_once ("lib/nusoap.php");
  2. $server = New soap_server ();
  3. //Avoid garbled characters
  4. $server ->soap_defencoding = ' UTF-8 ' ;
  5. $server ->decode_utf8 = false;
  6. $server ->xml_encoding = ' UTF-8 ' ;
  7. $server ->configurewsdl (' test '); //Open WSDL support
  8. /*
  9. registering programs that need to be accessed by clients
  10. type corresponding value: bool-> "Xsd:boolean" string-> "xsd:string"
  11. int-> "Xsd:int" float-> "Xsd:float "
  12. */
  13. $server ->register ( ' getteststr ', //Method name
  14. Array ("name" = " xsd:string" ), //parameter, default to "Xsd:string"
  15. Array ("return" = " xsd:string" )); //return value, default to "Xsd:string"
  16. //isset Detect if the variable is set
  17. $HTTP _raw_post_data = Isset ( $HTTP _raw_post_data )? $HTTP _raw_post_data : '' ;
  18. //service processing the data entered by the client
  19. $server ->service ( $HTTP _raw_post_data );
  20. /**
  21. * method for calling
  22. * @param $name
  23. */
  24. function getteststr ($name) {
  25. return "Hello, {$name}!" ;
  26. }
  27. ?>



3. Client: Establish the nusoapclient.php file.


[Php]view plaincopy

  1. require_once ("lib/nusoap.php");
  2. /*
  3. calling WebService via WSDL
  4. parameter 1 The address of the WSDL file (the WSDL after the question mark cannot be uppercase)
  5. Parameter 2 Specifies whether to use the WSDL
  6. $client = new SoapClient (' http://localhost/nusoapService.php?wsdl ', true);
  7. */
  8. $client = New soapclient ( ' http://localhost/nusoapService.php ' );
  9. $client ->soap_defencoding = ' UTF-8 ' ;
  10. $client ->decode_utf8 = false;
  11. $client ->xml_encoding = ' UTF-8 ' ;
  12. //parameter to array form pass
  13. $paras = Array (' name ' = ' Bruce Lee ' );
  14. //target method can omit subsequent arguments when no parameters are available
  15. $result = $client ->call ( ' getteststr ', $paras );
  16. //Check for error, get return value
  17. if (! $err = $client ->geterror ()) {
  18. Echo "return result:", $result;
  19. } Else {
  20. Echo "Call Error:", $err;
  21. }
  22. ?>


[Php]view plaincopy

  1. require_once ("lib/nusoap.php");
  2. /*
  3. calling WebService via WSDL
  4. parameter 1 The address of the WSDL file (the WSDL after the question mark cannot be uppercase)
  5. Parameter 2 Specifies whether to use the WSDL
  6. $client = new SoapClient (' http://localhost/nusoapService.php?wsdl ', true);
  7. */
  8. $client = New soapclient ( ' http://localhost/nusoapService.php?wsdl ', true);
  9. $client ->soap_defencoding = ' UTF-8 ' ;
  10. $client ->decode_utf8 = false;
  11. $client ->xml_encoding = ' UTF-8 ' ;
  12. //parameter to array form pass
  13. $paras = Array (' name ' = ' Bruce Lee ' );
  14. //target method can omit subsequent arguments when no parameters are available
  15. $client ->call ( ' getteststr ', $paras );
  16. $document = $client ->document;
  17. Echo $document;
  18. ?>


Note:Return Result: Hello, {Bruce Lee}!

Wsdl
WSDL is an XML language used to describe a Web service. It is a machine-readable format that provides all the information necessary to access a service to a Web service client. Nusoap specializes in providing a class for parsing WDSL files and extracting information from them. The SoapClient object uses a WSDL class to alleviate the difficulty of the developer invoking the service. With the help of the WSDL information to create the message, the programmer simply needs to know the name and parameters of the operation to invoke it.
Using WSDL with NUSOAP provides the following advantages:
All service meta-files, such as namespaces (namespaces), endpoint URLs, parameter names (parameter names), etc., can be obtained directly from the WSDL file, allowing the client to dynamically adapt to server-side changes. Because it is readily available from the server, this data no longer requires hard coding in user scripts.
It allows us to use the Soap_proxy class. This class derives from the SoapClient class and adds a method that corresponds to the operations detailed in the WDSL file. These methods can now be called directly by the user through it.
The SoapClient class contains a getproxy () square method that returns an object of a Soap_proxy class. The Soap_proxy class derives from the SoapClient class, adding a method that corresponds to the operation defined in the WSDL document, and allows the user to invoke a endpoint remote method. This applies only to cases where the SoapClient object is initialized with the Wdsl file. The advantage is that it is easy for users to use, and the disadvantage is that creating objects in performance--php is time-consuming-and does not serve utilitarian purposes (and this functionality serves no utilitarian purpose).


[Php]view plaincopy

  1. require_once ("lib/nusoap.php");
  2. $client = New soapclient ( ' http://localhost/nusoapService.php?wsdl ', true);
  3. $client ->soap_defencoding = ' UTF-8 ' ;
  4. $client ->decode_utf8 = false;
  5. $client ->xml_encoding = ' UTF-8 ' ;
  6. //Generate proxy class
  7. $proxy = $client ->getproxy ();
  8. //Call remote function
  9. $sq = $proxy ->getteststr (' Bruce Lee ');
  10. if (! $err = $proxy ->geterror ()) {
  11. Print_r ($sq);
  12. } Else {
  13. Print "ERROR: $err";
  14. }
  15. print ' REQUEST: '. <span>$p</span> <span>->request.</span> ' ' ;
  16. print ' RESPONSE: '. <span>Str_replace</span> <span>(</span><span>' ><</span>'<span>, </span> <span>' >\n<</span>'<span>, </span> <span>$p</span><span>->response).</span> ' ' ;
  17. ?>


Fourth step: Run Server Side file page: http://localhost/ Nusoapservice. PHP-Generated WSDL files

Click the method name. This allows us to provide a visual document to the service by using Nusoap by adding a few lines of code to the service. But that's not all we can do.

We add some WSDL calls in the service by using NUSOAP we can generate WSDL for the service and some other documentation. In contrast, there are few things we can do in the client, at least in our simple example. The client shown below is no different from the client without WSDL, the only difference being that parsing soapclent class is done by providing the URL to the WSDL, not the previous service endpoint.

Nusoap Call WebService the solution that appears garbled:

[Php]view plaincopy

    1. $client ->soap_defencoding = ' Utf-8 ' ;
    2. $client ->decode_utf8 = false;
    3. $client ->xml_encoding = ' Utf-8 ' ;

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

implement WebService with nusoap , do not open php SOAP extension, The reason is that Nusoap's soapclient class has conflicts with PHP5 's built-in soap classes.

Solution Solutions

1. Modify php.ini does not load PHP5 built-in SOAP extensions (Windows is Php_soap.dll).

2. There is also a renaming of the SoapClient class for Nusoap.

Identity verification

[Php]view plaincopy

  1. Header (' content-type:text/xml; Charset=utf-8 ');
  2. require_once (' nusoap.php ');
  3. $params = Array (' authenticationheader ' = = array(
  4. ' Content-type ' = ' text/xml; Charset=utf-8 ' ,
  5. ' SOAPAction ' = ' Yourfunstion ' ,
  6. )
  7. );
  8. $client = New nusoap_client (' http://www.yourdomain.com/service.asmx?wsdl ', True, ' , '', '', '');
  9. $client ->setheaders ('
  10. "Http://tempuri.org/webservice" >
  11. username
  12. Password
  13. ');
  14. $err = $client ->geterror ();
  15. if ($err) {
  16. Echo '

    Constructor Error

    '$err'
    ';
  17. }
  18. $result = $client ->call (' yourfunction ', $params, ', ' , False, true);
  19. if ($client->fault) {
  20. Echo '

    Fault

    ';  
  21. Print_r ($result);
  22. Echo '';
  23. } Else {
  24. $err = $client ->geterror ();
  25. if ($err) {
  26. Echo '

    Error

    '$err'
    ';
  27. } Else {
  28. Echo '

    Result

    ';  
  29. //print_r ($result);
  30. Echo '';
  31. }
  32. }
  33. Echo '

    Request

    ' . Htmlspecialchars ($client'
    ';
  34. Echo '

    Response

    ' . Htmlspecialchars ($client'
    ';
  35. ?>

The above describes the webservice--nusoap, including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

  • 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.