A PHP-created webservice, and a real instance called through C #

Source: Internet
Author: User
recently need to use PHP to create webservice for C # and Java to call, through 3 days of search and try, finally succeeded in C # down, Java call has not started, in order to prevent forgetting, here to record the whole process.

This article takes a look at a number of articles, as well as examples in this paper.

This article is in the following directory

First, PHP create WebService

Ii.. wsdl file generation

Third, C # calls PHP WebService

First, PHP create WebService

1. PHP Environment

I'm using the Apache,php.ini file in Windows Extension=php_soap.dll to get rid of comments, if Php_soap.dll is already installed.

Environment test, enter the following command in the CMD window to detect if soap is healthy

c:\>php--ini; used to see if the SOAP module in the INI file is normal, it does not see the same as Phpinfo ()

C:\>php-i |findstr "Soap" to see if the SOAP module is normal

C:\>php-r "New SoapClient (' http://localhost/wsdl/person.wsdl ');" ; Used to run soap directly

2. PHP Server file (testwebservice.php)

 PhpclassTestWebService { PublicfunctionHelloWorld () {returnArray("Helloworldresult" = "Welcome to Dongzi"); }        PublicfunctionGetArray ($args)          {            /*Note that the method of the Web service is declared at most one parameter, but when the method is called it must pass value1,value2 two arguments. (This is very confusing, and I understand that when the method is called, the system puts all the parameters in one object to pass over)*/$value 1=$args-value1; $value 2=$args->value2;//These two sentences are to get the real parameters$arry=Array($value 1,$value 2); //The return value is also very special, not to return $arry directly, but to put it in an object and return it. returnArray("Getarrayresult" =$arry); }  }  //Creating an Websevice instance$server=NewSoapServer ("testwebservice.wsdl"); //Specify class name$server->setclass ("TestWebService"); $server-handle (); ?>

View Code

Ii.. wsdl file generation

Originally I want to be simple, with SoapDiscovery.class.php to generate, but always have a variety of wonderful problems; forced to install Zend Studio12.5, but there are still other wonderful problems; Finally, I found a viable solution that was generated using VS2010, and there was no problem with C # calls.

1, open vs2010, new project-asp.net empty Web application;

2. Solution Explorer -Project (right-click)-Add-new Item-web service, name modified to Testwebservice.asmx, code as follows:

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSystem.Web.Services;namespaceWebapplication1{//////    testwebservice Summary description     ///[WebService (Namespace ="http://tempuri.org/")] [WebServiceBinding (ConformsTo=Wsiprofiles.basicprofile1_1)] [System.ComponentModel.ToolboxItem (false)]    //to allow the Web service to be called from the script using ASP. NET AJAX, uncomment the downstream. //[System.Web.Script.Services.ScriptService] PublicclassTestWebService:System.Web.Services.WebService {[WebMethod] PublicstringHelloWorld () {return""; } [WebMethod] Publicstring[] GetArray (stringAstringb) {returnNULL; }    }}

View Code

HelloWorld () and GetArray () are the methods we want to expose, and I write empty methods.

3, F5 Run, in the pop-up Internet Explorer to copy the running URL address

Http://localhost:63463/TestWebService.asmx

open a new IE browser window, paste and modify as, then enter, and the window will show the XML format content of the WSDL file:

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

4, click the file of IE browser-Save As menu, save as testwebservice.wsdl file, put in and testwebservice.php the same directory.

The code is as follows:

  XML version= "1.0" encoding= "Utf-8"?><wsdl:definitionsXmlns:soap= "http://schemas.xmlsoap.org/wsdl/soap/"Xmlns:tm= "http://microsoft.com/wsdl/mime/textMatching/"Xmlns:soapenc= "http://schemas.xmlsoap.org/soap/encoding/"Xmlns:mime= "http://schemas.xmlsoap.org/wsdl/mime/"Xmlns:tns= "http://tempuri.org/"xmlns:s= "Http://www.w3.org/2001/XMLSchema"XMLNS:SOAP12= "http://schemas.xmlsoap.org/wsdl/soap12/"xmlns:http= "http://schemas.xmlsoap.org/wsdl/http/"targetnamespace= "http://tempuri.org/"xmlns:wsdl= "http://schemas.xmlsoap.org/wsdl/"><Wsdl:types><S:schemaelementFormDefault= "qualified"targetnamespace= "http://tempuri.org/"><s:elementname= "HelloWorld"><S:complextype/>
   s:element><s:elementname= "Helloworldresponse"><S:complextype><s:sequence><s:elementminOccurs= "0"maxOccurs= "1"name= "Helloworldresult"type= "S:string"/>
    s:sequence>
     S:complextype>
      s:element><s:elementname= "GetArray"><S:complextype><s:sequence><s:elementminOccurs= "0"maxOccurs= "1"name= "a"type= "S:string"/><s:elementminOccurs= "0"maxOccurs= "1"name= "B"type= "S:string"/>
       s:sequence>
        S:complextype>
         s:element><s:elementname= "Getarrayresponse"><S:complextype><s:sequence><s:elementminOccurs= "0"maxOccurs= "1"name= "Getarrayresult"type= "Tns:arrayofstring"/>
          s:sequence>
           S:complextype>
            s:element><S:complextypename= "Arrayofstring"><s:sequence><s:elementminOccurs= "0"maxOccurs= "unbounded"name= "string"nillable= "true"type= "S:string"/>
             s:sequence>
              S:complextype>
               S:schema>
                Wsdl:types><Wsdl:messagename= "Helloworldsoapin"><Wsdl:partname= "Parameters"element= "Tns:helloworld"/>
                 Wsdl:message><Wsdl:messagename= "Helloworldsoapout"><Wsdl:partname= "Parameters"element= "Tns:helloworldresponse"/>
                  Wsdl:message><Wsdl:messagename= "Getarraysoapin"><Wsdl:partname= "Parameters"element= "Tns:getarray"/>
                   Wsdl:message><Wsdl:messagename= "Getarraysoapout"><Wsdl:partname= "Parameters"element= "Tns:getarrayresponse"/>
                    Wsdl:message><Wsdl:porttypename= "Testwebservicesoap"><wsdl:operationname= "HelloWorld"><Wsdl:inputmessage= "Tns:helloworldsoapin"/><Wsdl:outputmessage= "Tns:helloworldsoapout"/>
                     wsdl:operation><wsdl:operationname= "GetArray"><Wsdl:inputmessage= "Tns:getarraysoapin"/><Wsdl:outputmessage= "Tns:getarraysoapout"/>
                      wsdl:operation>
                       Wsdl:porttype><wsdl:bindingname= "Testwebservicesoap"type= "Tns:testwebservicesoap"><soap:bindingTransport= "Http://schemas.xmlsoap.org/soap/http"/><wsdl:operationname= "HelloWorld"><soap:operationSOAPAction= "Http://tempuri.org/HelloWorld"style= "Document"/><Wsdl:input><Soap:body Use= "literal"/>
                        Wsdl:input><Wsdl:output><Soap:body Use= "literal"/>
                         Wsdl:output>
                          wsdl:operation><wsdl:operationname= "GetArray"><soap:operationSOAPAction= "Http://tempuri.org/GetArray"style= "Document"/><Wsdl:input><Soap:body Use= "literal"/>
                           Wsdl:input><Wsdl:output><Soap:body Use= "literal"/>
                            Wsdl:output>
                             wsdl:operation>
                              wsdl:binding><wsdl:bindingname= "TESTWEBSERVICESOAP12"type= "Tns:testwebservicesoap"><soap12:bindingTransport= "Http://schemas.xmlsoap.org/soap/http"/><wsdl:operationname= "HelloWorld"><soap12:operationSOAPAction= "Http://tempuri.org/HelloWorld"style= "Document"/><Wsdl:input><Soap12:body Use= "literal"/>
                               Wsdl:input><Wsdl:output><Soap12:body Use= "literal"/>
                                Wsdl:output>
                                 wsdl:operation><wsdl:operationname= "GetArray"><soap12:operationSOAPAction= "Http://tempuri.org/GetArray"style= "Document"/><Wsdl:input><Soap12:body Use= "literal"/>
                                  Wsdl:input><Wsdl:output><Soap12:body Use= "literal"/> 
                                     wsdl:output> 
                                      wsdl:operation> 
                                       wsdl: Binding><wsdl:service  name= "TestWebService" > < Wsdl:port name = "Testwebservicesoap" binding = "Tns:testwebservicesoap" > < soap:address  Location = "Http://localhost:63463/TestWebService.asmx" />  
                                        wsdl:port> 
                                         wsdl:service >  
                                          wsdl:definitions >      

View Code

Change the http://localhost:63463/TestWebService.asmx to the URL you want to visit at the end:

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

Third, C # calls PHP WebService

1, new website-asp.net website;

2. Solution Explorer -Project (right-click)-Add a Web reference and enter it in the URL:

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 to:

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSystem.Web.UI;usingSystem.Web.UI.WebControls; PublicPartialclass_default:system.web.ui.page{protectedvoidPage_Load (Objectsender, EventArgs e) {        //Initialize WebServiceWebreference.testwebservice SRV =NewWebreference.testwebservice (); //The first method of tuningstringstr =srv.        HelloWorld (); //tune a second methodstring[] Arry = srv. GetArray ("string1","string2");    Response.Write (str); }}

View Code

4, Ctrl+f5 run, see the top appears "Welcome to Dongzi", indicating the successful call!

The above describes a PHP create webservice, and through the C # call real instance, including the aspects of the content, I hope to be interested in PHP tutorial friends helpful.

  • 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.