Home >Backend Development >PHP Tutorial >A real example of creating a webservice in php and calling it through c#

A real example of creating a webservice in php and calling it through c#

WBOY
WBOYOriginal
2016-07-29 09:15:11934browse

Recently, I need to use php to create a webservice for C# and JAVA to call. After 3 days of searching and trying, I finally successfully called it under C#. The JAVA call has not started yet. To prevent forgetting, the whole process is recorded here.

This article refers to many articles, and examples from them are also used in the article.

The directory of this article is as follows

1. PHP creates webservice

2. Generate .wsdl file

3. C# calls PHP webservice

A real example of creating a webservice in php and calling it through c#

1. PHP creates webservice

1. PHP environment

I am using Windows apache. Just remove the comment extension=php_soap.dll in the php.ini file, provided that php_soap.dll has been installed.

Environment test, enter the following command in the cmd window to check whether soap is normal

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)

A real example of creating a webservice in php and calling it through c#A real example of creating a webservice in php and calling it through c#

<span>php  
</span><span>class</span><span> TestWebService  
{  
    </span><span>public</span><span>function</span><span> HelloWorld()  
    {  
        </span><span>return</span><span>array</span>("HelloWorldResult"=>"welcome to dongzi world"<span>);  
    }  
 
    </span><span>public</span><span>function</span> GetArray(<span>$args</span><span>)  
        {  
          </span><span>/*</span><span>
           注意,Web Service的方法在声明时至多一个参数,  
            可是在调用该方法时就必须传value1,value2两个参数。  
            (这一点十分令人费解,我的理解是,在调用该方法时,系统把所有参数都放到一个<strong>对象</strong>里传过来的)  
          </span><span>*/</span><span>$value1</span> = <span>$args</span>-><span>value1;    
        </span><span>$value2</span> = <span>$args</span>->value2;<span>//</span><span>这两句是获取真正的参数  </span><span>$arry</span> = <span>array</span>(<span>$value1</span>,<span>$value2</span><span>);  
 
        </span><span>//</span><span>返回值也很特别,不是直接返回$arry,而是把它放到一个<strong>对象</strong>里再返回。  </span><span>return</span><span>array</span>("GetArrayResult"=><span>$arry</span><span>);  
    }  
}  
</span><span>//</span><span>创建WebSevice实例  </span><span>$server</span> = <span>new</span> SoapServer("TestWebService.wsdl"<span>);  
</span><span>//</span><span>指定类名  </span><span>$server</span>->setClass("TestWebService"<span>);  
</span><span>$server</span>-><span>handle();  
</span>?>
View Code

2. Generate .wsdl file

Originally I wanted to make it simpler and use SoapDiscovery .class.php, but all kinds of weird problems always occurred; I had to install Zend Studio 12.5, but other weird problems still appeared; finally I found a feasible solution, which is to use vs2010 to generate it, and there is no problem in calling C# .

1. Open vs2010, create a new project - ASP.NET empty Web application;

2. Solution Resource Manager - Project (right click) - Add - New Item - web service, change the name to TestWebService.asmx, The code is as follows:

A real example of creating a webservice in php and calling it through c#A real example of creating a webservice in php and calling it through c#

<span>using</span><span> System;
</span><span>using</span><span> System.Collections.Generic;
</span><span>using</span><span> System.Linq;
</span><span>using</span><span> System.Web;
</span><span>using</span><span> System.Web.Services;

</span><span>namespace</span><span> Web<strong>Application</strong>1
{
    </span><span>///</span><span><summary></summary></span><span>///</span><span> TestWebService 的摘要说明
    </span><span>///</span><span></span>    [WebService(Namespace = <span>"</span><span>http://tempuri.org/</span><span>"</span><span>)]
    [WebServiceBinding(ConformsTo </span>=<span> WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(</span><span>false</span><span>)]
    </span><span>//</span><span> 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
    </span><span>//</span><span> [System.Web.Script.Services.ScriptService]</span><span>public</span><span>class</span><span> TestWebService : System.Web.Services.WebService
    {

        [WebMethod]
        </span><span>public</span><span>string</span><span> HelloWorld()
        {
            </span><span>return</span><span>""</span><span>;
        }

        [WebMethod]
        </span><span>public</span><span>string</span>[] GetArray(<span>string</span> a,<span>string</span><span> b)
        {
            </span><span>return</span><span>null</span><span>;
        }
    }
}</span>
View Code

HelloWorld() and GetArray() are the methods we want to expose. I wrote them as empty methods.

3. Run F5, copy the running url address in the pop-up IE browser

http://localhost:63463/TestWebService.asmx

Open a new IE browser window, paste and change it to, Then press Enter. At this time, the window will display the xml format content of the wsdl file:

http://localhost:63463/TestWebService.asmx?wsdl

4. Click the file in the IE browser-Save as menu and save it as TestWebService. wsdl file, put it in the same directory as TestWebService.php.

The code is as follows:

A real example of creating a webservice in php and calling it through c#A real example of creating a webservice in php and calling it through c#

<span></span><span>xml version="1.0" encoding="utf-8"</span><span>?></span><span><span>wsdl:definitions </span><span>xmlns:soap</span><span>="http://schemas.xmlsoap.org/wsdl/soap/"</span><span> xmlns:tm</span><span>="http://microsoft.com/wsdl/mime/textMatching/"</span><span> xmlns:soapenc</span><span>="http://schemas.xmlsoap.org/soap/encoding/"</span><span> xmlns:mime</span><span>="http://schemas.xmlsoap.org/wsdl/mime/"</span><span> xmlns:tns</span><span>="http://tempuri.org/"</span><span> xmlns:s</span><span>="http://www.w3.org/2001/XMLSchema"</span><span> xmlns:soap12</span><span>="http://schemas.xmlsoap.org/wsdl/soap12/"</span><span> xmlns:http</span><span>="http://schemas.xmlsoap.org/wsdl/http/"</span><span> targetNamespace</span><span>="http://tempuri.org/"</span><span> xmlns:wsdl</span><span>="http://schemas.xmlsoap.org/wsdl/"</span><span>></span><span><span>wsdl:types</span><span>></span><span><span>s:schema </span><span>elementFormDefault</span><span>="qualified"</span><span> targetNamespace</span><span>="http://tempuri.org/"</span><span>></span><span><span>s:element </span><span>name</span><span>="HelloWorld"</span><span>></span><span><span>s:complexType </span><span>/></span><span></span><span>s:element</span><span>></span><span><span>s:element </span><span>name</span><span>="HelloWorldResponse"</span><span>></span><span><span>s:complexType</span><span>></span><span><span>s:sequence</span><span>></span><span><span>s:element </span><span>minOccurs</span><span>="0"</span><span> maxOccurs</span><span>="1"</span><span> name</span><span>="HelloWorldResult"</span><span> type</span><span>="s:string"</span><span>/></span><span></span><span>s:sequence</span><span>></span><span></span><span>s:complexType</span><span>></span><span></span><span>s:element</span><span>></span><span><span>s:element </span><span>name</span><span>="GetArray"</span><span>></span><span><span>s:complexType</span><span>></span><span><span>s:sequence</span><span>></span><span><span>s:element </span><span>minOccurs</span><span>="0"</span><span> maxOccurs</span><span>="1"</span><span> name</span><span>="a"</span><span> type</span><span>="s:string"</span><span>/></span><span><span>s:element </span><span>minOccurs</span><span>="0"</span><span> maxOccurs</span><span>="1"</span><span> name</span><span>="b"</span><span> type</span><span>="s:string"</span><span>/></span><span></span><span>s:sequence</span><span>></span><span></span><span>s:complexType</span><span>></span><span></span><span>s:element</span><span>></span><span><span>s:element </span><span>name</span><span>="GetArrayResponse"</span><span>></span><span><span>s:complexType</span><span>></span><span><span>s:sequence</span><span>></span><span><span>s:element </span><span>minOccurs</span><span>="0"</span><span> maxOccurs</span><span>="1"</span><span> name</span><span>="GetArrayResult"</span><span> type</span><span>="tns:ArrayOfString"</span><span>/></span><span></span><span>s:sequence</span><span>></span><span></span><span>s:complexType</span><span>></span><span></span><span>s:element</span><span>></span><span><span>s:complexType </span><span>name</span><span>="ArrayOfString"</span><span>></span><span><span>s:sequence</span><span>></span><span><span>s:element </span><span>minOccurs</span><span>="0"</span><span> maxOccurs</span><span>="unbounded"</span><span> name</span><span>="string"</span><span> nillable</span><span>="true"</span><span> type</span><span>="s:string"</span><span>/></span><span></span><span>s:sequence</span><span>></span><span></span><span>s:complexType</span><span>></span><span></span><span>s:schema</span><span>></span><span></span><span>wsdl:types</span><span>></span><span><span>wsdl:message </span><span>name</span><span>="HelloWorldSoapIn"</span><span>></span><span><span>wsdl:part </span><span>name</span><span>="parameters"</span><span> element</span><span>="tns:HelloWorld"</span><span>/></span><span></span><span>wsdl:message</span><span>></span><span><span>wsdl:message </span><span>name</span><span>="HelloWorldSoapOut"</span><span>></span><span><span>wsdl:part </span><span>name</span><span>="parameters"</span><span> element</span><span>="tns:HelloWorldResponse"</span><span>/></span><span></span><span>wsdl:message</span><span>></span><span><span>wsdl:message </span><span>name</span><span>="GetArraySoapIn"</span><span>></span><span><span>wsdl:part </span><span>name</span><span>="parameters"</span><span> element</span><span>="tns:GetArray"</span><span>/></span><span></span><span>wsdl:message</span><span>></span><span><span>wsdl:message </span><span>name</span><span>="GetArraySoapOut"</span><span>></span><span><span>wsdl:part </span><span>name</span><span>="parameters"</span><span> element</span><span>="tns:GetArrayResponse"</span><span>/></span><span></span><span>wsdl:message</span><span>></span><span><span>wsdl:portType </span><span>name</span><span>="TestWebServiceSoap"</span><span>></span><span><span>wsdl:operation </span><span>name</span><span>="HelloWorld"</span><span>></span><span><span>wsdl:input </span><span>message</span><span>="tns:HelloWorldSoapIn"</span><span>/></span><span><span>wsdl:output </span><span>message</span><span>="tns:HelloWorldSoapOut"</span><span>/></span><span></span><span>wsdl:operation</span><span>></span><span><span>wsdl:operation </span><span>name</span><span>="GetArray"</span><span>></span><span><span>wsdl:input </span><span>message</span><span>="tns:GetArraySoapIn"</span><span>/></span><span><span>wsdl:output </span><span>message</span><span>="tns:GetArraySoapOut"</span><span>/></span><span></span><span>wsdl:operation</span><span>></span><span></span><span>wsdl:portType</span><span>></span><span><span>wsdl:binding </span><span>name</span><span>="TestWebServiceSoap"</span><span> type</span><span>="tns:TestWebServiceSoap"</span><span>></span><span><span>soap:binding </span><span>transport</span><span>="http://schemas.xmlsoap.org/soap/http"</span><span>/></span><span><span>wsdl:operation </span><span>name</span><span>="HelloWorld"</span><span>></span><span><span>soap:operation </span><span>soapAction</span><span>="http://tempuri.org/HelloWorld"</span><span> style</span><span>="document"</span><span>/></span><span><span>wsdl:input</span><span>></span><span><span>soap:body </span><span>use</span><span>="literal"</span><span>/></span><span></span><span>wsdl:input</span><span>></span><span><span>wsdl:output</span><span>></span><span><span>soap:body </span><span>use</span><span>="literal"</span><span>/></span><span></span><span>wsdl:output</span><span>></span><span></span><span>wsdl:operation</span><span>></span><span><span>wsdl:operation </span><span>name</span><span>="GetArray"</span><span>></span><span><span>soap:operation </span><span>soapAction</span><span>="http://tempuri.org/GetArray"</span><span> style</span><span>="document"</span><span>/></span><span><span>wsdl:input</span><span>></span><span><span>soap:body </span><span>use</span><span>="literal"</span><span>/></span><span></span><span>wsdl:input</span><span>></span><span><span>wsdl:output</span><span>></span><span><span>soap:body </span><span>use</span><span>="literal"</span><span>/></span><span></span><span>wsdl:output</span><span>></span><span></span><span>wsdl:operation</span><span>></span><span></span><span>wsdl:binding</span><span>></span><span><span>wsdl:binding </span><span>name</span><span>="TestWebServiceSoap12"</span><span> type</span><span>="tns:TestWebServiceSoap"</span><span>></span><span><span>soap12:binding </span><span>transport</span><span>="http://schemas.xmlsoap.org/soap/http"</span><span>/></span><span><span>wsdl:operation </span><span>name</span><span>="HelloWorld"</span><span>></span><span><span>soap12:operation </span><span>soapAction</span><span>="http://tempuri.org/HelloWorld"</span><span> style</span><span>="document"</span><span>/></span><span><span>wsdl:input</span><span>></span><span><span>soap12:body </span><span>use</span><span>="literal"</span><span>/></span><span></span><span>wsdl:input</span><span>></span><span><span>wsdl:output</span><span>></span><span><span>soap12:body </span><span>use</span><span>="literal"</span><span>/></span><span></span><span>wsdl:output</span><span>></span><span></span><span>wsdl:operation</span><span>></span><span><span>wsdl:operation </span><span>name</span><span>="GetArray"</span><span>></span><span><span>soap12:operation </span><span>soapAction</span><span>="http://tempuri.org/GetArray"</span><span> style</span><span>="document"</span><span>/></span><span><span>wsdl:input</span><span>></span><span><span>soap12:body </span><span>use</span><span>="literal"</span><span>/></span><span></span><span>wsdl:input</span><span>></span><span><span>wsdl:output</span><span>></span><span><span>soap12:body </span><span>use</span><span>="literal"</span><span>/></span><span></span><span>wsdl:output</span><span>></span><span></span><span>wsdl:operation</span><span>></span><span></span><span>wsdl:binding</span><span>></span><span><span>wsdl:service </span><span>name</span><span>="TestWebService"</span><span>></span><span><span>wsdl:port </span><span>name</span><span>="TestWebServiceSoap"</span><span> binding</span><span>="tns:TestWebServiceSoap"</span><span>></span><span><span>soap:address </span><span>location</span><span>="http://localhost:63463/TestWebService.asmx"</span><span>/></span><span></span><span>wsdl:port</span><span>></span><span></span><span>wsdl:service</span><span>></span><span></span><span>wsdl:definitions</span><span>></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>
View Code

Change http://localhost:63463/TestWebService.asmx to the URL you ultimately want to visit, mine is:

http://192.168. 1.5/wsdl/006/TestWebService.php

3. C# calls PHP webservice

1. Create a new website - ASP.NET website;

2. SolutionResource Manager-Project (right-click)-Add Web reference, enter in the URL:

http://192.168.1.5/wsdl/006/TestWebService.php?wsdl

The following window will be displayed:

A real example of creating a webservice in php and calling it through c#

Click [Add Reference]

3. Modify The Default.aspx.cs code is:

A real example of creating a webservice in php and calling it through c#A real example of creating a webservice in php and calling it through c#

<span>using</span><span> System;
</span><span>using</span><span> System.Collections.Generic;
</span><span>using</span><span> System.Linq;
</span><span>using</span><span> System.Web;
</span><span>using</span><span> System.Web.UI;
</span><span>using</span><span> System.Web.UI.WebControls;

</span><span>public</span><span>partial</span><span>class</span><span> _Default : System.Web.UI.Page
{
    </span><span>protected</span><span>void</span> Page_Load(<span>object</span><span> sender, EventArgs e)
    {
        </span><span>//</span><span>初始化WebService  </span>        WebReference.TestWebService srv = <span>new</span><span> WebReference.TestWebService();
        </span><span>//</span><span>调第一个方法  </span><span>string</span> str =<span> srv.HelloWorld();
        </span><span>//</span><span>调第二个方法  </span><span>string</span>[] arry = srv.GetArray(<span>"</span><span>string1</span><span>"</span>, <span>"</span><span>string2</span><span>"</span><span>);
        Response.Write(str);
    }
}</span>
View Code

4. Run CTRL+F5 and see "welcome to dongzi world" appear at the top, indicating a successful call!

A real example of creating a webservice in php and calling it through c#

The above introduces a real example of creating a webservice in PHP and calling it through C#, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
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
Previous article:Predis set speed testNext article:Predis set speed test