How PHP invokes WebService application Introduction _php Tips

Source: Internet
Author: User
Tags soap soap client web services stock prices wsdl

1.1, the basic concept of WEB service

Web service also called XML Web Service WebService is a lightweight independent communication technology that can receive requests from the Internet or other systems on an intranet. is to use the software services provided by soap on the web, using a WSDL file for illustration and registering through UDDI.

XML: (extensible Markup Language) Extensible Markup Language. Short-term temporary data processing, web-oriented, is the basis of soap.

Soap: Simple Object Access Protocol (PROTOCOL). is the communication protocol for XML WEB Service. When a user finds your WSDL description document through UDDI, he can invoke one or more of the operations in the Web service that you established by soap. SOAP is the specification of a calling method in the form of an XML document that can support different underlying interfaces, such as HTTP (S) or SMTP.

WSDL: (Web Services Description Language) A WSDL file is an XML document that describes a set of SOAP messages and how to exchange them. Most of the cases are automatically generated and used by the software.

UDDI (Universal Description, Discovery, and integration) is a new project focused primarily on Web service providers and consumers. Before a user can invoke a Web service, it is necessary to determine which business methods are included in the service, to find the interface definition being invoked, and to compile the software on the server side, which is a mechanism for booting the system to find the appropriate service based on the description document. UDDI uses the SOAP message mechanism (standard xml/http) to publish, edit, browse, and find registration information. It encapsulates various types of data in an XML format and is sent to the registry or returned by the registry to return the required data.

1.2, the characteristics of XML Web service

The primary goal of WEB service is interoperability across platforms. To achieve this goal, the Web Service is a new platform for creating interoperable, distributed applications based entirely on platform-independent, software-independent standards, such as XML (Extensible Markup Language), XSD (XML Schema), and so on. Therefore, there are many advantages to using Web service:

1, communication across the firewall

If the application has thousands of users and is distributed around the world, communication between the client and the server will be a tricky issue. Because there is usually a firewall or proxy server between the client and the server. The traditional approach is to choose to use the browser as the client, write down a large pile of ASP pages, the application of the middle tier exposed to the end user. The result of this is that the development is difficult and the program is difficult to maintain. If client code is no longer so dependent on HTML forms, the client's programming is much simpler. If the middle-tier component is replaced with a Web service, the middle-tier component can be invoked directly from the user interface, eliminating the step of building an ASP page. To invoke a Web Service, you can directly use a SOAP client such as Microsoft SOAP Toolkit or. NET, or you can use a SOAP client that you develop, and then connect it to your application. Not only shortens the development cycle, but also reduces the complexity of the code and enhances the maintainability of the application. At the same time, the application no longer needs to jump to the corresponding results page each time the middle-tier component is invoked.

2. Application integration

Enterprise-Class application developers know that organizations often have to integrate programs that are written in different languages and run on different platforms, and this integration will take a lot of development power. Applications often need to get data from a program on a running host, or send data to a host or other platform application. Even on the same platform, different software vendors produce a variety of software also often need to integrate. With Web Service, applications can "expose" functionality and data to other applications using standard methods.

XML WEB Services provides the ability to exchange messages using standard protocols (HTTP, XML, SOAP, and WSDL) in a loosely coupled environment. The message can be structured, with a type, or it can be loosely defined.

3. Business-to-business Integration

Business-to-business refers to the e-commerce of merchants (generally referred to as enterprises) by Business to Business,as in businesses doing Business and other businesses, that is, between enterprises and enterprises through the Internet for products, Exchange of services and information. Popular parlance refers to e-commerce transactions between the supply and demand are businessmen (or enterprises, companies), they use the Internet technology or a variety of business network platform to complete the process of business transactions.

Web Service is the key to successful business-to-business integration. With Web service, companies can simply "expose" critical business applications to designated suppliers and customers, and Web service runs on the internet and can be easily implemented anywhere in the world, with relatively low operating costs. Web service is just a key part of business-to-business integration, and many other parts are needed to achieve integration. The biggest benefit of business-to-business integration with Web service is the ease with which interoperability can be achieved. As long as the business logic is "exposed" and becomes a Web Service, it allows any designated partner to invoke these business logic, regardless of the platform on which their system runs, and what development language they use. This greatly reduces the time and cost of spending on business-to-business integration.

4, software and data reuse

Web Service can reuse the data behind the code while allowing it to be reused. With Web service, it is no longer necessary to purchase, install, and then invoke components from a third party, and then call them from the application, just to call the remote Web Service directly. Another case of software reuse is that by integrating the functionality of several applications and "exposing" them through a web Service, it is easy to integrate all of these features into your portal and provide a unified, user-friendly interface for users. You can use the functionality provided by a Third-party Web service in your application, or you can make your application functionality available to others through a Web service. In both cases, the code and the data behind the code can be reused.

As you can see from the above discussion, Web Service is most useful when interoperating through the Web or remotely invoked. However, there are also situations where Web service does not offer any benefits at all, and the Web service has its drawbacks:

1, stand-alone applications

At present, enterprises and individuals also use a lot of desktop applications. Some of them only need to communicate with other programs on this computer. In this case, it is best not to use the Web Service, as long as the local API is OK. COM is ideal for working in this situation because it is small and fast. The same is true for server software running on the same server. Of course, Web Service can also be used on these occasions, but that would not only consume too much, but would not bring any benefits.

2, some of the local area network applications

In many applications, all programs are using COM under the Windows platform and running on the same LAN. In these programs, using DCOM is much more effective than soap/http. Similarly, if a. NET program is to connect to another. NET program on the local area network, you should use. NET Remoting. In the. NET remoting, you can also specify that you want to use Soap/http for Web Service calls. However, it is better to make RPC calls directly through TCP, which is much more effective.

1.3, the application of XML Web service

1. The initial XML Web Service is usually a source of information that can easily be incorporated into an application, such as stock prices, weather forecasts, sports scores, and so on.

2. Provide existing applications as XML Web service to build new, more powerful applications and leverage XML Web service as building blocks.

For example, a user can develop a sourcing application to automatically obtain price information from different vendors, allowing the user to select a vendor, submit an order, and then track the shipment until the goods are received. In addition to providing services on the Web, the vendor's application can use the XML Web service to check the customer's credit, collect the payment, and handle shipping formalities with the shipping company.

Second, the Web service development

Support for Web service is built into the. NET platform, including the construction and use of Web service. Unlike other development platforms, with the. NET platform, you do not need other tools or SDK to complete the development of Web service. The. NET Framework itself fully supports Web service, Includes server-side request processors and support for sending and receiving SOAP messages to clients. We're going to step this way. Create and use a simple Web Service with Microsoft Visual Studio. NET 20058 (hereafter referred to as Vs.net 2008).

2.1, with the creation of a simple web Service

First, open VS2005, open file-new-site, and select ASP.net Web service

Looking at the Service.cs code, you will find that Vs.net 2005 has established the default framework for Web service files. The original code is:

Copy Code code as follows:
Using System;


Using System.Linq;


Using System.Web;


Using System.Web.Services;


Using System.Web.Services.Protocols;


Using System.Xml.Linq;


[WebService (Namespace = "http://tempuri.org/")]


[WebServiceBinding (ConformsTo = wsiprofiles.basicprofile1_1)]


To allow the use of ASP.net AJAX to invoke this Web service from a script, uncomment the downlink.


[System.Web.Script.Services.ScriptService]


public class Service:System.Web.Services.WebService


{


Public Service () {


If you are using a design component, uncomment it to follow the line


InitializeComponent ();


}


[WebMethod]


public string HelloWorld ()


{


Return to "Hello World";


}


}


Using System;


Using System.Linq;


Using System.Web;


Using System.Web.Services;


Using System.Web.Services.Protocols;


Using System.Xml.Linq;


[WebService (Namespace = "http://tempuri.org/")]


[WebServiceBinding (ConformsTo = wsiprofiles.basicprofile1_1)]


To allow the use of ASP.net AJAX to invoke this Web service from a script, uncomment the downlink.


[System.Web.Script.Services.ScriptService]


public class Service:System.Web.Services.WebService


{


Public Service () {


If you are using a design component, uncomment it to follow the line


InitializeComponent ();


}


[WebMethod]


public string HelloWorld ()


{


Return to "Hello World";


}


}

The default project already has a Hello World method, run directly to see the effect,

Click the "HelloWorld" hyperlink on the display page to jump to the next page:

Click on the "Call" button to see the following graph of Web service results returned in XML format. This shows that our web service environment is not a problem, and we have a preliminary contact with the simplest Web service.

2.2. Create a simple and functional web Service

Above our macroscopic understanding of the webservice, in fact it is an external interface, which has functions for external customers to call (note: There is also a customer can not call the function). If we were the server, we wrote a webservice and gave it to the customer ( At the same time we gave them call rules), customers can get information from the server in a relatively transparent state. That is, customers do not understand (and do not need) their processes, they only get data. In the code file, if we write a function, we want this function to be an externally callable interface function, We have to add a line of code to the function [WebMethod (description= "function description")], if your function does not have this declaration, it will not be referenced by the user. Down we go to write a simple Web Service example.

First, the default HelloWorld method is commented out, the simple writing of the subtraction operation of the four methods;

Copy Code code as follows:
Using System;


Using System.Linq;


Using System.Web;


Using System.Web.Services;


Using System.Web.Services.Protocols;


Using System.Xml.Linq;


[WebService (Namespace = "http://tempuri.org/")]


[WebServiceBinding (ConformsTo = wsiprofiles.basicprofile1_1)]


To allow the use of ASP.net AJAX to invoke this Web service from a script, uncomment the downlink.


[System.Web.Script.Services.ScriptService]


public class Service:System.Web.Services.WebService


{


Public Service () {


If you are using a design component, uncomment it to follow the line


InitializeComponent ();


}


[WebMethod]


public string HelloWorld ()


//{


Return to "Hello World";


//}


[WebMethod (Description = "method of summation")]


Public double addition (double I, Double j)


{


return i + j;


}


[WebMethod (Description = "Method of finding Difference")]


Public double subtract (double I, Double j)


{


return i-j;


}


[WebMethod (Description = "method of Quadrature")]


public double multiplication (double I, Double j)


{


return I * j;


}


[WebMethod (Description = "Method of asking for business")]


Public Double division (double I, Double j)


{


if (J!= 0)


return i/j;


Else


return 0;


}


}



Copy Code code as follows:
Using System;


Using System.Linq;


Using System.Web;


Using System.Web.Services;


Using System.Web.Services.Protocols;


Using System.Xml.Linq;


[WebService (Namespace = "http://tempuri.org/")]


[WebServiceBinding (ConformsTo = wsiprofiles.basicprofile1_1)]


To allow the use of ASP.net AJAX to invoke this Web service from a script, uncomment the downlink.


[System.Web.Script.Services.ScriptService]


public class Service:System.Web.Services.WebService


{


Public Service () {


If you are using a design component, uncomment it to follow the line


InitializeComponent ();


}


[WebMethod]


public string HelloWorld ()


//{


Return to "Hello World";


//}


[WebMethod (Description = "method of summation")]


Public double addition (double I, Double j)


{


return i + j;


}


[WebMethod (Description = "Method of finding Difference")]


Public double subtract (double I, Double j)


{


return i-j;


}


[WebMethod (Description = "method of Quadrature")]


public double multiplication (double I, Double j)


{


return I * j;


}


[WebMethod (Description = "Method of asking for business")]


Public Double division (double I, Double j)


{


if (J!= 0)


return i/j;


Else


return 0;


}


}



Run can see our own written methods that can be invoked, as shown in the following figure:

Also click the addition method to enter the call page of the addition method.

Enter the parameter i=3,j=3 above the parameters, as shown above, click on the call to see the Web service results returned in XML format (the result of I and J added) the following figure

Here, we will find that, in fact, WebService is not so mysterious, it is only an interface, for us, the focus is the compilation of interface functions.

2.3. Call Web Service with asp.net
First, open VS2005, open file-new-site, and select ASP. NET Web site ".

Select the storage location, click OK after the language, and go to the default page. Then add the Web reference first and webservice the current project. Right-click in Explorer, select Add Web Reference, and bring up the dialog box:

Fill in the URL, the previous written webservice after the browser to run the address shown above (that is, webservice after the release of the Access address), click the "Go" button, as shown above, will show the referenced webservice can be invoked in the method, and then click "Add Reference" , the WebService reference to the current project, the following figure, the solution will appear in the introduction of the WebService file

We're here to practice calling WebService's four methods, make a simple invocation example, first add a few controls to the front of the site, the code is as follows:

Copy Code code as follows:
<%@ Page language= "C #" autoeventwireup= "true" codefile= "Default.aspx.cs" inherits= "_default"%>


<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">


<html xmlns= "http://www.w3.org/1999/xhtml" >


<head runat= "Server" >


<title> Untitled Page </title>


</head>


<body>


<form id= "Form1" runat= "Server" >


<div>


<asp:textbox id= "NUM1" runat= "Server" ></asp:TextBox>


<select id= "selectoper" runat = "server" >


<option>+</option>


<option>-</option>


<option>*</option>


<option>/</option>


</select>


<asp:textbox id= "Num2" runat= "Server" ></asp:TextBox>


<asp:button id= "Button1" runat= "server" text= "=" onclick= "Button1_Click"/>


<asp:textbox id= "Result" runat= "server" ></asp:TextBox>


</div>


</form>


</body>


</html>


<%@ Page language= "C #" autoeventwireup= "true" codefile= "Default.aspx.cs" inherits= "_default"%>


<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">


<html xmlns= "http://www.w3.org/1999/xhtml" >


<head runat= "Server" >


<title> Untitled Page </title>


</head>


<body>


<form id= "Form1" runat= "Server" >


<div>


<asp:textbox id= "NUM1" runat= "Server" ></asp:TextBox>


<select id= "selectoper" runat = "server" >


<option>+</option>


<option>-</option>


<option>*</option>


<option>/</option>


</select>


<asp:textbox id= "Num2" runat= "Server" ></asp:TextBox>


<asp:button id= "Button1" runat= "server" text= "=" onclick= "Button1_Click"/>


<asp:textbox id= "Result" runat= "server" ></asp:TextBox>


</div>


</form>


</body>


</html>



Then write the calling code in the background, before the call is instantiated, just as you would with other objects, and the instantiation method is localhost. Service a = new localhost. Service (), and then you can access the method provided in WebService through a. In this example, a button control is dynamically created to trigger a webservice call, and the background code is as follows:
After running, you can see the effect, as shown in the following figure, enter two operands in the previous two textbox, select the operator in the Middle Drop-down list, and click the "=" number to output the result of the calculation to the third textbox.

The entire calculation is not done locally, is calculated on the Web server and then returns the result through XML to the caller, so, when running the program, the WebService program must also be started, otherwise it will report an exception that cannot connect to the remote server, as shown in the following figure:

The background code is as follows:

Copy Code code as follows:
Using System;


Using System.Configuration;


Using System.Data;


Using System.Linq;


Using System.Web;


Using System.Web.Security;


Using System.Web.UI;


Using System.Web.UI.HtmlControls;


Using System.Web.UI.WebControls;


Using System.Web.UI.WebControls.WebParts;


Using System.Xml.Linq;


public partial class _default:system.web.ui.page


{


protected void Page_Load (object sender, EventArgs e)


{


}


protected void Button1_Click (object sender, EventArgs e)


{


string selectflag = Selectoper.value;


localhost. Service web = new localhost. Service ();


if (selectflag.equals ("+"))


{


Result.text = (web.addition (double). Parse (Num1.text), double. Parse (Num2.text))). ToString ();


}


else if (selectflag.equals ("-"))


{


Result.text = (web.subtract (double). Parse (Num1.text), double. Parse (Num2.text))). ToString ();


}


else if (selectflag.equals ("*"))


{


Result.text = (web.multiplication (double). Parse (Num1.text), double. Parse (Num2.text))). ToString ();


}


else if (selectflag.equals ("/"))


{


Result.text = (web.division (double). Parse (Num1.text), double. Parse (Num2.text))). ToString ();


}


}


}



Copy Code code as follows:
Using System;


Using System.Configuration;


Using System.Data;


Using System.Linq;


Using System.Web;


Using System.Web.Security;


Using System.Web.UI;


Using System.Web.UI.HtmlControls;


Using System.Web.UI.WebControls;


Using System.Web.UI.WebControls.WebParts;


Using System.Xml.Linq;


public partial class _default:system.web.ui.page


{


protected void Page_Load (object sender, EventArgs e)


{


}


protected void Button1_Click (object sender, EventArgs e)


{


string selectflag = Selectoper.value;


localhost. Service web = new localhost. Service ();


if (selectflag.equals ("+"))


{


Result.text = (web.addition (double). Parse (Num1.text), double. Parse (Num2.text))). ToString ();


}


else if (selectflag.equals ("-"))


{


Result.text = (web.subtract (double). Parse (Num1.text), double. Parse (Num2.text))). ToString ();


}


else if (selectflag.equals ("*"))


{


Result.text = (web.multiplication (double). Parse (Num1.text), double. Parse (Num2.text))). ToString ();


}


else if (selectflag.equals ("/"))


{


Result.text = (web.division (double). Parse (Num1.text), double. Parse (Num2.text))). ToString ();


}


}


}



To this a simple webservice development and call has been completed, in practical applications can according to their own needs, write some powerful, complex webservice, no matter how complex, the whole process is the case.

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.