: This article describes how to create a webservice using php and call a real instance using c #. For more information about PHP tutorials, see. Recently, we need to use php to create webservice for C # and JAVA to call. After three days of searching and trying, we finally successfully called it under C #. JAVA has not started yet, to prevent forgetting, record the entire process here.
This article references many articles and uses examples.
The contents of this article are as follows:
1. create a webservice in php
II. Generate A. wsdl file
III. c # Call php webservice
1. create a webservice in php
1. php environment
I am using windows apache, and the extension = php_soap.dll in the php. ini file can be removed from the annotation, provided that php_soap.dll has been installed.
Run the following command in the cmd window to check whether soap works.
C: \> php -- ini; used to check whether the soap module in the ini file is normal. it may not be consistent with what phpinfo () sees.
C: \> php-I | findstr "soap"; used to check whether the soap module is normal
C: \> php-r "new SoapClient ('http: // localhost/wsdl/person. wsdl ');"; used to directly run soap
2. php Server File (TestWebService. php)
"Welcome to dongzi world");} publicfunction GetArray ($ args) {/* Note that the Web Service method has one more parameter to declare, however, when calling this method, the value1 and value2 parameters must be passed. (This is confusing at 01:10. in my understanding, when calling this method, the system places all the parameters in oneObject) */$ Value1 = $ args-> value1; $ value2 = $ args-> value2; // These two statements are used to obtain the true parameter $ arry = array ($ value1, $ value2); // the returned value is also very special. instead of directly returning $ arry, it is put intoObject. Returnarray ("GetArrayResult" => $ arry) ;}// create a WebSevice instance $ server = new SoapServer ("TestWebService. wsdl "); // specify the class name $ server-> setClass (" TestWebService "); $ server-> handle ();?>
View Code
II. Generate A. wsdl file
Originally, I wanted to make it easier to use SoapDiscovery. class. php is generated, but there are always various strange issues. Zend Studio12.5 has to be installed, but another strange problem still occurs. In the end, I found a feasible solution that was generated using vs2010, C # The call is correct.
1. open vs2010 and create a project-ASP. NET empty Web application;
2. solutionsResource Manager-Project (right-click)-Add-new item-web service, and change the name to TestWebService. asmx. the code is as follows:
Using System; using System. Collections. Generic; using System. Linq; using System. Web; using System. Web. Services; namespace WebApplication1 {////// Summary of TestWebService ///[WebService (Namespace = "http://tempuri.org/")] [WebServiceBinding (ConformsTo = WsiProfiles. basicProfile1_1)] [System. componentModel. toolboxItem (false)] // to allow ASP. net ajax calls this Web service from the script. Please cancel the comments to the downstream. // [System. web. script. services. scriptService] publicclass TestWebService: System. web. services. webService {[WebMethod] publicstring HelloWorld () {return "";} [WebMethod] publicstring [] GetArray (string a, string B) {returnnull ;}}}
View Code
HelloWorld () and GetArray () are the methods we want to expose. I have written empty methods.
3. run F5. copy the running url in the pop-up IE browser.
Http: // localhost: 63463/TestWebService. asmx
Open a New IE browser window, paste and modify it to, and press Enter. the window displays the xml format of the wsdl file:
Http: // localhost: 63463/TestWebService. asmx? Wsdl
4. click File in IE browser-save as menu, save as TestWebService. wsdl file, and put it in the same directory as TestWebService. php.
The code is as follows:
View Code
Modify http: // localhost: 63463/TestWebService. asmx to the URL you finally want to visit. my website is:
Http: // 192.168.1.5/wsdl/006/TestWebService. php
III. c # Call php webservice
1. create a website-ASP. NET website;
2. solutionsResource Manager-Project (right-click)-Add a Web reference and enter:
Http: // 192.168.1.5/wsdl/006/TestWebService. php? Wsdl
The following window is displayed:
Click [add reference]
3. modify the Default. aspx. cs code:
Using System; using System. collections. generic; using System. linq; using System. web; using System. web. UI; using System. web. UI. webControls; publicpartialclass _ Default: System. web. UI. page {protectedvoid Page_Load (object sender, EventArgs e) {// initialize WebService WebReference. testWebService srv = new WebReference. testWebService (); // call the first method string str = srv. helloWorld (); // call the second method string [] arry = srv. getArray ("string1", "string2"); Response. write (str );}}
View Code
4. run CTRL + F5. if "welcome to dongzi world" is displayed on the top, the call is successful!
The above describes how to create a webservice in php and call a real instance through c #, including some content. I hope my friends who are interested in the PHP Tutorial can help me.