Use pear: soap to create a web service

Source: Internet
Author: User
Use pear: soap to create a web service

  1. Class FruitQuoteService
  2. {
  3. Public $ __dispatch_map = array ();
  4. Public $ __typedef = array ();
  5. Public function FruitQuoteService ()
  6. {
  7. $ This->__ dispatch_map ['getquote'] = array (
  8. "In" => array ("category" => "string "),
  9. "Out" => array ("quote" => "int ")
  10. );
  11. $ This->__ dispatch_map ['getresult'] = array (
  12. "In" => array (),
  13. "Out" => array ("fruitSummary" => "{urn: FruitQuoteService} fruitStruct ")
  14. );
  15. $ This->__ typedef ['fruitstruct '] = array (
  16. 'Category '=> 'string', 'amount' => 'int'
  17. );
  18. }
  19. Public function getQuote ($ category)
  20. {
  21. Switch ($ category)
  22. {
  23. Case 'apple ':
  24. $ Quote = 10;
  25. Break;
  26. Case 'Orange ':
  27. $ Quote = 12;
  28. Break;
  29. Case 'bana ':
  30. $ Quote = 20;
  31. Break;
  32. Default:
  33. $ Quote = 0;
  34. Break;
  35. }
  36. Return $ quote;
  37. } // End funtion
  38. Public function getFruit ()
  39. {
  40. $ List = array (
  41. Array ("apple", 100 ),
  42. Array ("orange", 500 ),
  43. Array ("banana", 260)
  44. );
  45. Return $ list;
  46. } // End funtion
  47. } // End class
  48. ?>

Step 2: Create the server. php program to receive and process client requests.

  1. Require_once ("FruitQuoteService. php ");
  2. Require_once ("SOAP/Server. php ");
  3. $ FruitQuote = new FruitQuoteService ();
  4. $ Server = new Soap_Server ();
  5. $ Server-> addObjectMap ($ fruitQuote, "http://www.xxx.com ");
  6. If (isset ($ _ SERVER ['request _ method']) & $ _ SERVER ['request _ method'] = 'post ')
  7. {
  8. $ Server-> service ($ GLOBALS ['http _ RAW_POST_DATA ']);
  9. } Else
  10. {
  11. Require_once 'soap/Disco. php ';
  12. $ Disco = new SOAP_DISCO_Server ($ server, 'fruitquoteservice ');
  13. Header ("Content-type: text/xml ");
  14. If (isset ($ _ SERVER ['query _ string']) & strcasecmp ($ _ SERVER ['query _ string'], 'wsdl ') = 0 ){
  15. Echo $ disco-> getWSDL ();
  16. } Else {
  17. Echo $ disco-> getDISCO ();
  18. }
  19. }
  20. Exit;
  21. ?>

Can Now via http://www.shangyong.com/ws/server.php? View the wsdl document in wsdl.

DISCO: a Microsoft technology used to publish and discover Web services. it defines a simple http get mechanism for obtaining web service descriptions from a given url.

Step 3: Create web service client code

  1. Require_once ('soap/Client. php ');
  2. // The namespace must be consistent with the one defined in server. php.
  3. $ Options = array ('namespace' => 'http: // www.xxx.com ',
  4. 'Track' => 1); // if it is set to 1, the soap message can be obtained through _ get_wire. the default value is 0.
  5. $ Client = new SOAP_client ("http://www.shangyong.com/ws/server.php ");
  6. $ Params = array ();
  7. $ Response = $ client-> call ("getFruit", $ params, $ options );
  8. // Print_r ($ client->__ get_wire (); // output the soap message
  9. If (PEAR: isError ($ response )){
  10. Echo 'Error: '. $ response-> getMessage ()."
    \ N ";
  11. } Else {
  12. Print_r ($ response). "\ n ";
  13. }
  14. $ Params = array ("name" => "orange ");
  15. $ Response = $ client-> call ("getQuote", $ params, $ options );
  16. If (PEAR: isError ($ response )){
  17. Echo 'Error: '. $ response-> getMessage ()."
    \ N ";
  18. } Else {
  19. Echo $ response. "\ n ";
  20. }
  21. Exit;
  22. ?>

Client code 2

  1. Require_once ('soap/Client. php ');
  2. /**
  3. * All service content, such as namespace, UEL, and parameter names, can be obtained from the wsdl file.
  4. */
  5. $ Wsdl = new SOAP_WSDL ("http://www.shangyong.com/ws/server.php? Wsdl ");
  6. /**
  7. * Generate a proxy object from the wsdl, which contains all the operation methods defined in the wsdl document.
  8. * Functions can be directly called through proxy objects.
  9. * Advantages: easy to use
  10. */
  11. $ Client = $ wsdl-> getProxy ();
  12. $ Response = $ client-> getQuote ("apple ");
  13. If (PEAR: isError ($ response )){
  14. Echo 'Error: '. $ response-> getMessage ()."
    \ N ";
  15. } Else {
  16. Echo $ response. "\ n ";
  17. }
  18. $ Response = $ client-> getFruit ();
  19. If (PEAR: isError ($ response )){
  20. Echo 'Error: '. $ response-> getMessage ()."
    \ N ";
  21. } Else {
  22. Print_r ($ response). "\ n ";
  23. }
  24. Exit;
  25. ?>

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.