- Class FruitQuoteService
- {
- Public $ __dispatch_map = array ();
- Public $ __typedef = array ();
- Public function FruitQuoteService ()
- {
- $ This->__ dispatch_map ['getquote'] = array (
- "In" => array ("category" => "string "),
- "Out" => array ("quote" => "int ")
- );
- $ This->__ dispatch_map ['getresult'] = array (
- "In" => array (),
- "Out" => array ("fruitSummary" => "{urn: FruitQuoteService} fruitStruct ")
- );
-
- $ This->__ typedef ['fruitstruct '] = array (
- 'Category '=> 'string', 'amount' => 'int'
- );
- }
-
- Public function getQuote ($ category)
- {
- Switch ($ category)
- {
- Case 'apple ':
- $ Quote = 10;
- Break;
- Case 'Orange ':
- $ Quote = 12;
- Break;
- Case 'bana ':
- $ Quote = 20;
- Break;
- Default:
- $ Quote = 0;
- Break;
- }
- Return $ quote;
- } // End funtion
-
- Public function getFruit ()
- {
- $ List = array (
- Array ("apple", 100 ),
- Array ("orange", 500 ),
- Array ("banana", 260)
- );
- Return $ list;
- } // End funtion
- } // End class
- ?>
Step 2: Create the server. php program to receive and process client requests.
- Require_once ("FruitQuoteService. php ");
- Require_once ("SOAP/Server. php ");
- $ FruitQuote = new FruitQuoteService ();
- $ Server = new Soap_Server ();
- $ Server-> addObjectMap ($ fruitQuote, "http://www.xxx.com ");
- If (isset ($ _ SERVER ['request _ method']) & $ _ SERVER ['request _ method'] = 'post ')
- {
- $ Server-> service ($ GLOBALS ['http _ RAW_POST_DATA ']);
- } Else
- {
- Require_once 'soap/Disco. php ';
- $ Disco = new SOAP_DISCO_Server ($ server, 'fruitquoteservice ');
- Header ("Content-type: text/xml ");
- If (isset ($ _ SERVER ['query _ string']) & strcasecmp ($ _ SERVER ['query _ string'], 'wsdl ') = 0 ){
- Echo $ disco-> getWSDL ();
- } Else {
- Echo $ disco-> getDISCO ();
- }
- }
- Exit;
- ?>
-
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
- Require_once ('soap/Client. php ');
- // The namespace must be consistent with the one defined in server. php.
- $ Options = array ('namespace' => 'http: // www.xxx.com ',
- 'Track' => 1); // if it is set to 1, the soap message can be obtained through _ get_wire. the default value is 0.
- $ Client = new SOAP_client ("http://www.shangyong.com/ws/server.php ");
- $ Params = array ();
- $ Response = $ client-> call ("getFruit", $ params, $ options );
- // Print_r ($ client->__ get_wire (); // output the soap message
- If (PEAR: isError ($ response )){
- Echo 'Error: '. $ response-> getMessage ()."
\ N ";
- } Else {
- Print_r ($ response). "\ n ";
- }
- $ Params = array ("name" => "orange ");
- $ Response = $ client-> call ("getQuote", $ params, $ options );
- If (PEAR: isError ($ response )){
- Echo 'Error: '. $ response-> getMessage ()."
\ N ";
- } Else {
- Echo $ response. "\ n ";
- }
- Exit;
- ?>
Client code 2
- Require_once ('soap/Client. php ');
- /**
- * All service content, such as namespace, UEL, and parameter names, can be obtained from the wsdl file.
- */
- $ Wsdl = new SOAP_WSDL ("http://www.shangyong.com/ws/server.php? Wsdl ");
- /**
- * Generate a proxy object from the wsdl, which contains all the operation methods defined in the wsdl document.
- * Functions can be directly called through proxy objects.
- * Advantages: easy to use
- */
- $ Client = $ wsdl-> getProxy ();
- $ Response = $ client-> getQuote ("apple ");
- If (PEAR: isError ($ response )){
- Echo 'Error: '. $ response-> getMessage ()."
\ N ";
- } Else {
- Echo $ response. "\ n ";
- }
- $ Response = $ client-> getFruit ();
- If (PEAR: isError ($ response )){
- Echo 'Error: '. $ response-> getMessage ()."
\ N ";
- } Else {
- Print_r ($ response). "\ n ";
- }
- Exit;
- ?>
|