Introduction to how php calls webservice applications

高洛峰
Release: 2023-03-02 17:16:01
Original
1208 people have browsed it

1.1. Basic concepts of Web Service

Web Service is also called XML Web Service. WebService is a lightweight independent communication technology that can receive requests passed from the Internet or other systems on the Intranet. Yes: A software service delivered over the Web via SOAP, described using a WSDL file, and registered via UDDI.

XML: (Extensible Markup Language) Extensible Markup Language. For short-term temporary data processing and the World Wide Web, it is the foundation of Soap.

Soap: (Simple Object Access Protocol) Simple Object Access Protocol. It is the communication protocol of XML Web Service. When the user finds your WSDL description document through UDDI, he can call one or more operations in the Web service you created through SOAP. SOAP is a specification for calling methods in the form of XML documents. It 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. In most cases automatically generated and used by software.

UDDI (Universal Description, Discovery, and Integration) is a new project mainly aimed at Web service providers and users. Before users can call a Web service, they must determine which business methods are included in the service, find the called interface definition, and compile software on the server side. UDDI is a mechanism that guides the system to find the corresponding service based on the description document. UDDI uses SOAP messaging mechanism (standard XML/HTTP) to publish, edit, browse and find registration information. It uses XML format to encapsulate various types of data, and sends it to the registration center or the registration center returns the required data.

1.2. Characteristics of XML Web Service

The main goal of Web Service is cross-platform interoperability. In order to achieve this goal, Web Service is completely based on platform-independent and software vendor-independent standards such as XML (Extensible Markup Language) and XSD (XML Schema). It is a new platform for creating interoperable and distributed applications. . Therefore, using Web Service has many advantages:

1. Communication across firewalls

If the application has thousands of users and is distributed around the world, then the communication between the client and the server will be a thorny problem . 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 a lot of ASP pages, and expose the middle layer of the application to the end user. The result of this is that development is difficult and the program is difficult to maintain. Client-side programming would be much simpler if client-side code no longer relied so much on HTML forms. If the middle-tier component is replaced by a Web Service, the middle-tier component can be called directly from the user interface, thus eliminating the step of creating an ASP page. To call a Web Service, you can directly use a SOAP client such as Microsoft SOAP Toolkit or .net, or you can use a self-developed SOAP client and then connect it to the application. Not only does it shorten the development cycle, it also reduces code complexity and enhances the maintainability of the application. At the same time, the application no longer needs to jump to the corresponding "result page" every time it calls the middle-tier component.

2. Application integration

Enterprise-level application developers all know that enterprises often have to integrate various programs written in different languages ​​and running on different platforms, and this integration will cost a lot of money. Great development power. Applications often need to obtain data from a program running on a host; or send data to the host or other platform applications. Even on the same platform, various software produced by different software vendors often need to be integrated. Through Web Services, applications can use standard methods to "expose" functions and data for use by other applications.

XML Web services provide the ability to exchange messages using standard protocols (HTTP, XML, SOAP and WSDL) in a loosely coupled environment. Messages can be structured, typed, or loosely defined.

3. B2B integration

B2B refers to Business to Business, as in businesses doing business with other businesses, merchants (generally refers to enterprises) to merchants e-commerce, that is, products and services between enterprises through the Internet and exchange of information. In layman's terms, it means that both the supply and demand sides of e-commerce transactions are merchants (or enterprises, companies), and they use Internet technology or various business network platforms to complete the process of business transactions.

Web Service is the key to successful B2B integration. Through Web Service, companies can simply "expose" key business applications to designated suppliers and customers. Web Service runs on the Internet and can be easily implemented anywhere in the world, and its operating costs are relatively low. Low. Web Service is only a key part of B2B integration, and many other parts are needed to achieve integration. The biggest advantage of using Web Service to implement B2B integration is that interoperability can be easily achieved. As long as the business logic is "exposed" and becomes a Web Service, any designated partner can call these business logic, regardless of what platform their system runs on or what development language they use. This greatly reduces the time and cost spent on B2B integration.

4. Software and data reuse

Web Service allows reuse of code while also reusing the data behind the code. Using Web Service, you no longer have to purchase and install software components from a third party as before, and then call these components from the application; you only need to directly call the remote Web Service. Another situation of software reuse is to integrate the functions of several applications and "expose" them through Web Service. You can easily integrate all these functions into your portal site to provide users with a unified , friendly interface. You can use the functions provided by third-party Web Services in your applications, or you can provide your own application functions to others through Web Services. In both cases, the code and the data behind the code can be reused.

As can be seen from the above discussion, Web Service is most useful when performing interoperation or remote calls through the Web. However, there are also some situations where Web Service cannot bring any benefits at all. Web Service has the following disadvantages:

1. Stand-alone applications

Currently, enterprises and individuals still use many desktop applications. Some of them simply need to communicate with other programs on the machine. In this case, it is best not to use Web Service, just use the local API. COM is ideal for working in this situation because it is small and fast. The same goes for server software running on the same server. Of course, Web Service can also be used in these situations, but that will not only consume too much, but will not bring any benefits.

2. Some applications on LAN

In many applications, all programs use COM under the Windows platform and run on the same LAN. In these programs, using DCOM will be much more efficient than SOAP/HTTP. Similarly, if a .net program wants to connect to another .net program on the LAN, .net Remoting should be used. In fact, in .net Remoting, you can also specify to use SOAP/HTTP to make Web Service calls. However, it is better to make RPC calls directly through TCP, which is much more efficient.

1.3. Application of XML Web Service

1. The original XML Web Service is usually an information source that can be easily incorporated into the application, such as stock prices, weather forecasts, sports scores, etc.

2. Providing existing applications as XML Web Services allows you to build new, more powerful applications and leverage XML Web Services as building blocks.

For example, users can develop a procurement application to automatically obtain price information from different suppliers, allowing users to select suppliers, submit orders, and then track the shipment of goods until they are received. In addition to providing services on the Web, the supplier's application can also use XML Web Service to check the customer's credit, collect payment, and handle freight procedures with the freight company.

2. Web Service Development

.net platform has built-in support for Web Service, including the construction and use of Web Service. Unlike other development platforms, using the .net platform, you do not need other tools or SDKs to complete the development of Web Service. The .net Framework itself fully supports Web Services, including server-side request processors and support for clients to send and receive SOAP messages. Next, we will use Microsoft Visual Studio .net 20058 (hereinafter referred to as VS.Net 2008) step by step to create and use a simple Web Service.

2.1. Create the simplest Web Service using

First, open VS2005, open "File-New-Website", select "ASP.NET Web Service"

View the Service.cs code, you will find that VS .Net 2005 has established a default framework for Web Service files. The original code is:

Copy the code The code is as follows: using System;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System. (Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this web service to be called from a script using ASP.NET AJAX, please uncheck the following line Note.
// [System.Web.Script.Services.ScriptService]
public class Service : System.Web.Services.WebService
{
public Service () {
If you use the designed component, please uncomment the following line
/ /InitializeComponent();
}
[WebMethod] public string HelloWorld()
{ return "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 this web service to be called from a script using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class Service: System.Web.Services.WebService
{
     public Service () {
                                                                                                                                                             /InitializeComponent();
}
[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
}

2.2. Create a simple functional Web Service

Our macro understanding above In fact, it is an external interface with functions that can be called by external clients (note: there are also functions that cannot be called by clients). If we are the server, we write a webservice and then give it to Clients (and we gave them calling rules), the clients can be in a relatively transparent state when getting information from the server. Even if the clients don't understand (and don't need) the process, they only get the data. In the code file , if we write a function and want this function to become an externally callable interface function, we must add a line of code [WebMethod(Description="function description information")] to the function. If your function does not have this declaration, It will not be referenced by users. Next we start to write a simple Web Service example.

First comment out the default HelloWorld method, and simply write four methods for addition, subtraction, multiplication and division;

Copy the code The code is 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 this web service to be called from a script using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class Service : System.Web.Services.WebService
{
public Service () {
//If you use the designed component, please uncomment the following line
/ /InitializeComponent();                                                                                                                     

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!