. A typical example of net tuning PHP Web Service

Source: Internet
Author: User
Tags db2 php server wsdl
A recent project was ported to WinForm through the PHP Web service to access DB2 by the WinForm Direct access DB2.

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

The difficulty of this proposition is not to visit DB2, but to. NET calls PHP's Web Service. For my long-term. NET, I've always thought it was only. NET can do Web Service ... People, it is really a bit strong "cong" difficult.

But the problem is still to be solved, the deadline is put in front of it. After some investigation, finally have the prospect, now share to everybody.

First of all, the PHP server needs at least two files-a WSDL file and a php file. A WSDL file is a machine-readable XML file that describes the service and invocation methods provided by WebService (which can be used to generate the calling code automatically for. net), and PHP files are really Web services that are implemented.

1) PHP server-side code

1-1) testwebservice.php Code

The following is the referenced content:

<?php
Class TestWebService
{
Public Function HelloWorld ()
{
Return Array ("Helloworldresult" = "Hello");
}

Public Function GetArray ($args)
{
/*
Note that the method of the Web service is declared at a maximum of one parameter,
However, you must pass value1,value2 two parameters when calling this method.
(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 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.
Return Array ("getarrayresult" = $arry);
}
}

Creating an Websevice instance
$server = new SoapServer ("testwebservice.wsdl");
Specify Class name
$server->setclass ("TestWebService");

$server->handle ();

?>


1-2) TESTWEBSERVICE.WSDL Code

The following is the referenced content:

<?xml version= "1.0" encoding= "Utf-8"?>
<wsdl:definitions xmlns: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:schema elementformdefault= "qualified" targetnamespace= "http://tempuri.org/" >
<s:element name= "HelloWorld" >
<s:complextype/>
</s:element>
<s:element name= "Helloworldresponse" >
<s:complexType>
<s:sequence>
<s:element minoccurs= "0" maxoccurs= "1" name= "Helloworldresult" type= "s:string"/>
</s:sequence>
</s:complexType>
</s:element>
<s:element name= "GetArray" >
<s:complexType>
<s:sequence>
<s:element minoccurs= "0" maxoccurs= "1" name= "value1" type= "s:string"/>
<s:element minoccurs= "0" maxoccurs= "1" name= "value2" type= "s:string"/>
</s:sequence>
</s:complexType>
</s:element>
<s:element name= "Getarrayresponse" >
<s:complexType>
<s:sequence>
<s:element minoccurs= "0" maxoccurs= "1" name= "Getarrayresult" type= "tns:arrayofstring"/>
</s:sequence>
</s:complexType>
</s:element>
<s:complextype name= "Arrayofstring" >
<s:sequence>
<s:element minoccurs= "0" maxoccurs= "unbounded" name= "string" nillable= "true" type= "s:string"/>
</s:sequence>
</s:complexType>
</s:schema>
</wsdl:types>
<wsdl:message name= "Helloworldsoapin" >
<wsdl:part name= "Parameters" element= "Tns:helloworld"/>
</wsdl:message>
<wsdl:message name= "Helloworldsoapout" >
<wsdl:part name= "Parameters" element= "Tns:helloworldresponse"/>
</wsdl:message>
<wsdl:message name= "Getarraysoapin" >
<wsdl:part name= "Parameters" element= "Tns:getarray"/>
</wsdl:message>
<wsdl:message name= "Getarraysoapout" >
<wsdl:part name= "Parameters" element= "Tns:getarrayresponse"/>
</wsdl:message>
<wsdl:porttype name= "Testwebservicesoap" >
<wsdl:operation name= "HelloWorld" >
<wsdl:input message= "Tns:helloworldsoapin"/>
<wsdl:output message= "Tns:helloworldsoapout"/>
</wsdl:operation>
<wsdl:operation name= "GetArray" >
<wsdl:input message= "Tns:getarraysoapin"/>
<wsdl:output message= "Tns:getarraysoapout"/>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name= "Testwebservicesoap" type= "Tns:testwebservicesoap" >
<soap:binding transport= "Http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name= "HelloWorld" >
<soap:operation soapaction= "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:operation name= "GetArray" >
<soap:operation soapaction= "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:binding name= "TESTWEBSERVICESOAP12" type= "Tns:testwebservicesoap" >
<soap12:binding transport= "Http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name= "HelloWorld" >
<soap12:operation soapaction= "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:operation name= "GetArray" >
<soap12:operation soapaction= "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/phpmyadmin/ws/TestWebService.php"/>
</wsdl:port>
<wsdl:port name= "TESTWEBSERVICESOAP12" binding= "TNS:TESTWEBSERVICESOAP12" >
<soap12:address location= "http://localhost/phpmyadmin/ws/TestWebService.php"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>


The code for the WSDL is long, and when the method is many, it is unlikely that the hand is knocking the code. There is a clever way, is also used. NET implements a Web Serivce that does not contain a real method body, and then through Http://***/testwebservice.asmx? WSDL method to generate a WSDL code file.

With regard to the WSDL file, I would like to state the special note two:

(1) Soap:address node is the address of the declaration webservice, at the time of deployment to change to the corresponding address;

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

2). NET client code

To add a Web reference, the address is the HTTP address of the WSDL file.

Calling code (C #)

The following is the referenced content:

Initialize WebService
localhost. TestWebService srv = new localhost. TestWebService ();
The first method of tuning
String str = srv. HelloWorld ();
Tune a second method
String[] arry= srv. GetArray ("string1", "string2");


Summary: (a) PHP is a weak type of language, it is difficult to check errors. The array type is also different from the generally understood array, and it also has a usage similar to Hashtable.

(b) The input parameters and return values of the PHP Web service method are at most one, because the parameters and return values that are actually called are wrapped in an object.

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

(iv) If the return value needs to be a number of two-dimensional tables, I am shallow to think that you can transfer a set of string arrays, formatted as

[Table 1 row number],[table 1 column],[table 1 column name 1],[table 1 column name 2], ... [Table 1 Column Name n],[value in table 1 by columns

[Table 2 row number],[table 2 column],[table 2 column name 1],[table 2 column Name 2], ... [Table 2 column name n],[value in table 2 by columns

......

[Table m number of rows],[table M column],[table m column name 1],[table m column name 2], ... [Table m column name n],[value in table 2 by rows and columns]

The above is. NET tune PHP Web Service Typical examples of content, more related articles please pay attention to topic.alibabacloud.com (www.php.cn)!

  • Related Article

    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.