How PHP Calls WebService application Introduction _php Tutorial

Source: Internet
Author: User
Tags soap client stock prices wsdl
1.1. Basic concept of WEB service

Web service, also known as XML Web service WebService, is a lightweight, independent communication technology that can receive requests passed from the Internet or other systems on the intranet. is: Software services provided through SOAP on the web, described using WSDL files, and registered through UDDI.

XML: (extensible Markup Language) Extensible Markup Language. Temporary data processing for short-term, web-oriented network, 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 actions in the Web service that you set up by soap. SOAP is a specification for calling methods in the form of XML documents 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. In most cases, it is automatically generated and used by the software.

UDDI (Universal Description, Discovery, and integration) is a new project primarily for Web service providers and users. Before a user can invoke a Web service, it is important to determine which business methods are included in the service, to find the called interface definition, and to prepare the software on the server side, and UDDI is a mechanism to guide the system through the description document to find the appropriate service. UDDI uses the SOAP message mechanism (standard xml/http) to publish, edit, browse, and find registration information. It uses XML format to encapsulate various types of data and send it to the registry or to the registry to return the required data.

1.2. Characteristics of XML Web service

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

1. Cross-firewall communication

If the application has thousands of users and is distributed around the world, then 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 use a browser as a client, write down a lot of ASP pages, and expose the middle tier of the application to the end user. The result 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 called 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 your own SOAP client and then connect it to your application. It not only shortens the development cycle, but also reduces the complexity of the code and improves 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 called.

2. Application integration

Enterprise-Class application developers know that it is common for businesses to integrate various programs that are written in different languages and run on different platforms, and that this integration will take a lot of development power. Applications often need to get data from programs running on one host, or send data to a host or other platform application. Even on the same platform, a variety of software produced by different software vendors often need to be integrated. With Web Service, applications can "expose" functionality and data in a standard way for use by other applications.

XML WEB Services provides 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, business-to-business integration

Business-to-business-to-business,as in businesses doing businesses with other businesses, businesses (generally referred to enterprises) of business e-commerce, that is, between enterprises and enterprises through the Internet products, Exchange of services and information. Popular parlance refers to the trade between the supply and demand of e-commerce business (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 the success of 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 greatest benefit of using Web service for business-to-business integration is the ease of interoperability. As long as the business logic is "exposed" and becomes a Web Service, it is possible for any designated partner to invoke these business logic, regardless of the platform on which their system runs, and what language to 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 reuse of the code. With Web service, it is no longer necessary to purchase, install, and invoke these components from a third party, just call the remote Web service directly, as you did before. Another kind of software reuse is the integration of the functions of several applications, through the Web Service "exposed", it is very easy to integrate all these features into your portal site, to provide users with a unified, user-friendly interface. You can use the functionality provided by a third-party Web service in your application, or you can make your own application functionality available to others through a Web service. In both cases, the data behind the code and the code can be reused.

As you can see from the above discussion, Web Service is most useful when it is interoperable or remotely invoked over the Web. However, there are cases where Web service does not bring any benefit at all, and Web service has some drawbacks:

1. Stand-alone application

Today, businesses and individuals are also using many 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. COM is well suited to working in this situation because it is both small and fast. This is also true for server software running on the same server. Of course, Web Service can also be used in these situations, but that not only consumes too much, but does not bring any benefits.

2. Some applications of LAN

In many applications, all programs use COM under the Windows platform, all 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 fact, in. NET remoting, you can also specify that you use Soap/http to make Web Service calls. However, it is best to make RPC calls directly over TCP, which is much more effective.

1.3. Application of XML Web service

1. The initial XML Web Service is often a source of information that can easily be incorporated into the 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 take advantage of XML Web service as building blocks.

For example, a user can develop a sourcing application to automatically obtain price information from different vendors, so that users can select suppliers, submit orders, and then track the shipment of the goods until they receive the goods. In addition to providing services on the Web, the vendor's application can also use an XML Web service to check the customer's credit, collect the purchase price, and handle shipping formalities with the shipping company.

Second, Web service development

The. NET platform has built-in support for Web service, including the building 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 service, Includes server-side request handlers and support for sending and receiving SOAP messages to clients. We'll step through it. Create and use a simple Web Service using Microsoft Visual Studio. NET 20058 (hereafter referred to as Vs.net 2008).

2.1. Create one of the simplest web Service

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

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

Copy CodeThe 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 the Web service to be called from the script using ASP. NET AJAX, uncomment the downstream.
[System.Web.Script.Services.ScriptService]
public class Service:System.Web.Services.WebService
{
Public Service () {
If you are using a design component, uncomment the 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 the Web service to be called from the script using ASP. NET AJAX, uncomment the downstream.
[System.Web.Script.Services.ScriptService]
public class Service:System.Web.Services.WebService
{
Public Service () {
If you are using a design component, uncomment the line
InitializeComponent ();
}
[WebMethod]
public string HelloWorld ()
{
Return "Hello World";
}
}

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

Click the "HelloWorld" hyperlink in the show page to jump to the next page:

Click on the "Invoke" button to see the Web service results returned in XML format. Explains that our web service environment is not a problem, but also initial exposure to the simplest Web service.

2.2. Create a simple Web Service with functionality

Above we understand the macro-webservice, in fact, it is an external interface, there is a function for external customers to call (note: There is also a client can not call function). If we were the server, we wrote a webservice and gave it to the customer ( At the same time we give them the call rules), the client can be in a relatively transparent state when obtaining information from the server. That is, the customer does not understand (and does not need) its process, they only get the data. In the code file, if we write a function, we want this function to be an externally callable interface function. We must add a line of code to the function [WebMethod (description of the description= function)], and if your function does not have this declaration, it will not be referenced by the user. Down we started writing a simple Web Service example.

The default HelloWorld method is commented out first, and the four methods of subtraction operation are simply written.

Copy CodeThe 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 the Web service to be called from the script using ASP. NET AJAX, uncomment the downstream.
[System.Web.Script.Services.ScriptService]
public class Service:System.Web.Services.WebService
{
Public Service () {
If you are using a design component, uncomment the line
InitializeComponent ();
}
[WebMethod]
public string HelloWorld ()
//{
Return "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 Seeking quotient")]
Public Double division (double I, Double j)
{
if (J! = 0)
return i/j;
Else
return 0;
}
}
Copy CodeThe 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 the Web service to be called from the script using ASP. NET AJAX, uncomment the downstream.
[System.Web.Script.Services.ScriptService]
public class Service:System.Web.Services.WebService
{
Public Service () {
If you are using a design component, uncomment the line
InitializeComponent ();
}
[WebMethod]
public string HelloWorld ()
//{
Return "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 Seeking quotient")]
Public Double division (double I, Double j)
{
if (J! = 0)
return i/j;
Else
return 0;
}
}

Run can see the methods we write ourselves that can be called, such as:

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

Enter the parameter i=3,j=3 on the parameter, for example, click Invoke to see the Web service results returned in XML format (the result of the addition of I and J)

Here, we will find that, in fact, WebService is not so mysterious, it is just an interface, for us, the focus is to write the interface function.

2.3. Calling Web Service with ASP.
First, open VS2005, open file-new-Web 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 and bring the WebService to the current project. By right-clicking in the Explorer and selecting Add Web Reference, bring up the dialog box:

Fill in the URL, the previously written webservice the address shown above the browser (ie: WebService post-release access address), click on the "Go" button, such as will show the referenced webservice can be called method, and then click "Add Reference", The webservice is referenced to the current project, for example, the solution will appear in the introduction of the WebService file

Here we practice calling WebService's four methods, make a simple invocation of the example, first in the site's foreground to add a few controls, the code is as follows:

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



No title page





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



No title page





Then write the calling code in the background, before invoking it and using other objects, instantiate it first, and instantiate it as localhost. Service a = new localhost. Service (); Then you can access the method provided in WebService via a. In this example, the dynamic creation of a button control to trigger the webservice call, the background code is as follows:
After running, you can see the effect, as shown in the preceding two textbox, enter two operands, select the operator in the Middle drop-down list, and then click the "=" sign to output the computed result to the third textbox.

The entire calculation is not done locally, it is computed on the Web server and then the result is returned to the caller through XML, so, when running the program, the WebService program must also be started, otherwise it will not be able to connect to the remote servers exception, such as:

The background code is as follows:

Copy CodeThe code is 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 CodeThe code is 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 invocation has been completed, in the actual application can according to their own needs, write some powerful, complex webservice, no matter how complex, the whole process is like this.

http://www.bkjia.com/PHPjc/326273.html www.bkjia.com true http://www.bkjia.com/PHPjc/326273.html techarticle 1.1. Web Service basic Concept Web service is also called XML Web Service WebService is a kind of request that can receive from the Internet or other systems on the intranet, lightweight independent ...

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