. NET tuning PHP Web service is a typical example

Source: Internet
Author: User
Tags array arrays object net php file php server php code string
A recent project was ported by WinForm Direct Access DB2 to the WinForm access DB2 through the PHP Web service. (The advantage is that PHP can be on Linux, and Linux is free) The difficulty of this proposition is not access to DB2, but. NET calls PHP's Web Service. For this long I have been thinking only of. NET. NET can do Web Service ... People, is really a little strong "cong" difficult.

But the problem is still to be solved, the deadline is placed in front of. After some investigation, finally has the prospect, now shares 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 services and invocation methods provided by WebService (for. NET can automatically generate calling code, which is very useful), and PHP files are truly Web services implemented.

1 PHP server-side code

1-1) testwebservice.php Code

The following are the referenced contents:

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

Public Function GetArray ($args)
{
/*
Note that the Web service method is declared at most one parameter,
However, when this method is invoked, two parameters must be passed value1,value2.
(This is very puzzling, and my understanding is that when this method is invoked, the system puts all parameters in an object).
*/

$value 1 = $args->value1;
$value 2 = $args->value2;//These two sentences are to get the real argument

$arry = Array ($value 1, $value 2);

The return value is also special, not returning directly to $arry, but putting it in an object and returning it.
Return Array ("Getarrayresult" => $arry);
}
}

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

$server->handle ();


?>


1-2) TESTWEBSERVICE.WSDL Code

The following are the referenced contents:

<?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 relatively long, and it is unlikely that the hand-knocking code is possible when the method is many. There is a clever way, is also used. NET implements a Web Serivce that does not contain a real method body, and then passes through Http://***/testwebservice.asmx? WSDL method to generate a WSDL code file.

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

(1) The Soap:address node is the address of the declaration webservice, which should be changed to the corresponding address at the time of deployment;

(2) The declaration type of a one-dimensional array is arrayoftype and the string array is arrayofstring. If type is not a simple type, then type requires a separate declaration.

2). NET client code

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

Calling code (C #)

The following are the referenced contents:

Initialize WebService
localhost. TestWebService srv = new localhost. TestWebService ();
Adjust the first method
String str = srv. HelloWorld ();
Adjust the second method
String[] arry= srv. GetArray ("string1", "string2");


Summary: (a) PHP is a weak type language, check error is more difficult. The array type is also different from the generally understood array, and it also has a similar hashtable usage.

(b) The PHP Web service method has an incoming parameter and a return value of at most one, because the parameters and return values of the actual invocation are wrapped into an object.

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

(d) If the return value needs to be more than one two-dimensional table, I shallow think that you can transfer a set of string array transfer, in the form of

[Table 1 row number],[table 1 columns],[table 1 list 1],[table 1 column 2], ... [Table 1 column names n],[by rows and columns in table 1]

[Table 2 row number],[table 2 columns],[table 2 list 1],[table 2 column 2], ... [Table 2 column names n],[by rows and columns in table 2]

......

[Table m row number],[table m columns],[table m column name 1],[table m column name 2], ... [Table m Column name n],[table 2 values by rows and columns]

In order to string the contents of the above [] into an array of strings, the efficiency is good, I tested 10000 rows of 240 columns of data, I have ready-made code, interested can be obtained from me.



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.