Home > Backend Development > C#.Net Tutorial > Typical example of calling PHP Web Service using .NET

Typical example of calling PHP Web Service using .NET

黄舟
Release: 2016-12-15 13:34:13
Original
1096 people have browsed it

A recent project was ported from "WinForm directly accessing DB2" to "WinForm accessing DB2 through PHP Web Service".

(The advantage is that PHP can be run on Linux, and Linux is free)

The difficulty of this proposition is not accessing DB2, but .NET calling PHP's Web Service. For me, a person who has been working with .NET for a long time, and who has always thought that only .NET can do Web Service..., this is really a bit difficult for me.

But the problem still needs to be solved, and the deadline is right in front of us. After some investigation, I finally got a clue, and now I share it with you.

First of all, the PHP server requires at least two files - a WSDL file and a PHP file. The WSDL file is a machine-readable XML file that is used to describe the services and calling methods provided by WebService (for .NET, the calling code can be automatically generated, which is very easy to use). The php file is the real WEB service.

1) PHP server-side code

1-1) TestWebService.php code

The following is the quoted content:

class TestWebService
{
  public function HelloWorld()
  {
    return array(" HelloWorldResult"=>"Hello"); }

} public function GetArray($args)

                                                 method must be passed There are two parameters, value1 and value2. (This is very puzzling. My understanding is that when calling this method, the system puts all parameters in one object)
*/

$ value1 = $ args- & gt; value1 ;
                                                                                              arry, but put it into an object and return it.
           return array("GetArrayResult"=>$arry);
     }

}


//Create a WebSevice instance
$server = new SoapServer("TestWebService.wsdl");
//Specify the class name

$server-> ;setClass("TestWebService");


$server->handle();

?>



1-2) TestWebService.wsdl code

The following is the quoted content:



 
   
     
       
     

     
       
         
           
         

       

     

     
       
         
           
           
         

       

     

     
       
         
           
         

       

     

     
       
         
       

     

   

 

 
   
 

 
   
 

 
   
 

 
   
 

 
   
     
     
   

   
     
     
   

 

 
   
   
     
     
       
     

     
       
     

   

   
     
     
       
     

     
       
     

   

 

 
   
   
     
     
       
     

     
       
     

   

   
     
     
       
     

     
       
     

   

 

 
   
     
   

   
     
   

 



WSDL code is relatively long. When there are many methods, it is impossible to type the code by hand. There is a clever way, which is to use .NET to implement a Web Service without a real method body, and then use http://***/TestWebService.asmx? The wsdl method generates a wsdl code file.

Regarding the WSDL file, I would like to explain two special points:

(1) soap:address node is the address where the WebService is declared, which should be changed to the corresponding address when deploying;

(2) The declaration type of the one-dimensional array is ArrayOfType, and the string array is ArrayOfString. If Type is not a simple type, Type needs to be declared separately.

2) .NET client code

First add a Web reference, the address is the Http address of the WSDL file. Call the code (C#)

Below is a reference content:

// initialize webService

LocalHost.testWebService SRV = New LocalHost.testwebService (); ( ;); . The array type is also different from the generally understood array. It also has usage similar to Hashtable.


(2) The PHP Web Service method can have at most one incoming parameter and return value, because the parameters and return value when actually called are all packaged and transmitted in an object.

(3) PHP Web Service also supports complex types such as custom types and custom type arrays, but does not support multiple arrays.

(4) If the return value needs to be multiple two-dimensional tables, I superficially thought that a set of string arrays can be transferred. The format is

[number of rows in table 1], [number of columns in table 1], [number of columns in table 1] 1 column name 1], [Table 1 column name 2], ... [Table 1 column name N], [Values ​​stored in rows and columns in Table 1]


[Table 2 row number], [Table 2 column number], [Table 2 column name 1], [Table 2 column name 2], ... [Table 2 column name N], [Table 2 values ​​stored in rows and columns]

...

[Table M row number], [ Table M column number], [Table M column name 1], [Table M column name 2], ... [Table M column name N], [Values ​​stored in rows and columns in Table 2]

The above is .NET PHP adjustment Contents of typical examples of Web Service. For more related articles, please pay attention to the PHP Chinese website (m.sbmmt.com)!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template