Introduction to how php calls webservice application_PHP tutorial

WBOY
Release: 2016-07-21 15:14:33
Original
971 people have browsed it

1.1、Web Service基本概念

Web Service也叫XML Web Service WebService是一种可以接收从Internet或者Intranet上的其它系统中传递过来的请求,轻量级的独立的通讯技术。是:通过SOAP在Web上提供的软件服务,使用WSDL文件进行说明,并通过UDDI进行注册。

XML:(Extensible Markup Language)扩展型可标记语言。面向短期的临时数据处理、面向万维网络,是Soap的基础。

Soap:(Simple Object Access Protocol)简单对象存取协议。是XML Web Service 的通信协议。当用户通过UDDI找到你的WSDL描述文档后,他通过可以SOAP调用你建立的Web服务中的一个或多个操作。SOAP是XML文档形式的调用方法的规范,它可以支持不同的底层接口,像HTTP(S)或者SMTP。

WSDL:(Web Services Description Language) WSDL 文件是一个 XML 文档,用于说明一组 SOAP 消息以及如何交换这些消息。大多数情况下由软件自动生成和使用。

UDDI (Universal Description, Discovery, and Integration) 是一个主要针对Web服务供应商和使用者的新项目。在用户能够调用Web服务之前,必须确定这个服务内包含哪些商务方法,找到被调用的接口定义,还要在服务端来编制软件,UDDI是一种根据描述文档来引导系统查找相应服务的机制。UDDI利用SOAP消息机制(标准的XML/HTTP)来发布,编辑,浏览以及查找注册信息。它采用XML格式来封装各种不同类型的数据,并且发送到注册中心或者由注册中心来返回需要的数据。

1.2、XML Web Service的特点

Web Service的主要目标是跨平台的可互操作性。为了实现这一目标,Web Service 完全基于XML(可扩展标记语言)、XSD(XML Schema)等独立于平台、独立于软件供应商的标准,是创建可互操作的、分布式应用程序的新平台。因此使用Web Service有许多优点:

1、跨防火墙的通信

如果应用程序有成千上万的用户,而且分布在世界各地,那么客户端和服务器之间的通信将是一个棘手的问题。因为客户端和服务器之间通常会有防火墙或者代理服务器。传统的做法是,选择用浏览器作为客户端,写下一大堆ASP页面,把应用程序的中间层暴露给最终用户。这样做的结果是开发难度大,程序很难维护。 要是客户端代码不再如此依赖于HTML表单,客户端的编程就简单多了。如果中间层组件换成Web Service的话,就可以从用户界面直接调用中间层组件,从而省掉建立ASP页面的那一步。要调用Web Service,可以直接使用Microsoft SOAP Toolkit或.net这样的SOAP客户端,也可以使用自己开发的SOAP客户端,然后把它和应用程序连接起来。不仅缩短了开发周期,还减少了代码复杂度,并能够增强应用程序的可维护性。同时,应用程序也不再需要在每次调用中间层组件时,都跳转到相应的"结果页"。

2、应用程序集成

企业级的应用程序开发者都知道,企业里经常都要把用不同语言写成的、在不同平台上运行的各种程序集成起来,而这种集成将花费很大的开发力量。应用程序经常需要从运行的一台主机上的程序中获取数据;或者把数据发送到主机或其它平台应用程序中去。即使在同一个平台上,不同软件厂商生产的各种软件也常常需要集成起来。通过Web Service,应用程序可以用标准的方法把功能和数据"暴露"出来,供其它应用程序使用。

XML Web services 提供了在松耦合环境中使用标准协议(HTTP、XML、SOAP 和 WSDL)交换消息的能力。消息可以是结构化的、带类型的,也可以是松散定义的。

3、B2B的集成

B2B 指的是Business to Business,as in businesses doing business with other businesses,商家(泛指企业)对商家的电子商务,即企业与企业之间通过互联网进行产品、服务及信息的交换。通俗的说法是指进行电子商务交易的供需双方都是商家(或企业、公司),她们使用了Internet的技术或各种商务网络平台,完成商务交易的过程。

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 can reuse the data behind the code while allowing code reuse. 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 and provide users with a unified , friendly interface. You can use the functions provided by third-party Web Services in your application, or you can provide your own application functions to others through Web Service. 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 interoperating or making 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 application

Currently, businesses and individuals still use many desktop applications. Some of them only 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 would not only consume too much, but also not bring any benefits.

2. Some applications of 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 a source of information 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" and select "ASP.NET Web Service"

Introduction to how php calls webservice application_PHP tutorial

Look at 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 code The code is as follows:
using System;
using System.Linq;
using System.Web;
using System.Web.Services; Xml.Linq;
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow the use of ASP.NET AJAX To call this web service from a script, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class Service : System.Web.Services.WebService
{
public Service () {
//If using design 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 To allow calling this web service 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 using design For components, please cancel the following:
// initializecomponent ();
}
[webmedhod]
Public String HelloWorld ()
{
Return "Hello World";
}
}

There is already a Hello World method in the default project, run it directly to see the effect,

Introduction to how php calls webservice application_PHP tutorialClick the "HelloWorld" hyperlink in the picture on the display page to jump to the next page:

Introduction to how php calls webservice application_PHP tutorialClick the "Call" button, and you can see the Web Service result returned in XML format as shown below. It shows that there is no problem with our Web Service environment, and we also had preliminary contact with the simplest Web Service.

2.2. Create a simple Web Service with functions

Above we have a macro understanding of webservice. In fact, it is an external interface. There are functions in it that can be called by external customers (note: there are also functions that cannot be called by customers). If we are the server, we have written it a webservice, and then give it to the client (at the same time we give them the calling rules), the client can be in a relatively transparent state when obtaining information from the server. Even if the client does not understand (and does not need) the process, they Only get 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="Description information of the function")] above the function, If your function does not have this declaration, it will not be referenced by users. Let's start writing a simple Web Service example.

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

Copy code The code is as follows:
using System;
using System.Linq;
using System.Web;
using System.Web.Services; Xml.Linq;
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow the use of ASP.NET AJAX To call this web service from a script, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class Service : System.Web.Services.WebService
{
public Service () {
//If using design Component, please uncomment the following line
//InitializeComponent();
}
//[WebMethod]
//public string HelloWorld()
//{
// return "Hello World";
//}
[WebMethod(Description = "Sum Method")]
public double addition(double i, double j)
{
return i + j;
}
[WebMethod(Description = "Method to find the difference")]
public double subtract(double i, double j)
{
return i - j;
}                                                                                                                                                                                                                                                    ; [WebMethod(Description = "Method of finding quotient")]
public double division(double i, double j)
{ if (j != 0)
return i / j;
                                                                              return 0;                                                           
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 call this web service from a script, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class Service: System.Web.Services.WebService
{
public Service () {
//If using design Component, please uncomment the following lines
                    // InitializeComponent(); return "Hello World";
//}
[WebMethod(Description = "Sum Method")]
public double addition(double i, double j)
{
return i + j;
}
[WebMethod(Description = "Method to find the 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 finding quotient")]
public double division(double i, double j)
{
if (j != 0)
return i / j;
else
return 0;
}
}



When you run it, you can see the methods we wrote that can be called, as shown below:

Similarly click on the addition method to enter the calling page of the addition method.

Introduction to how php calls webservice application_PHP tutorial

Enter the parameters i=3, j=3 above the parameters, as shown in the picture above, click Call, and you can see the Web Service result returned in XML format (the result of adding i and j) as shown below

At this point, we will find that webservice is not that mysterious. It is just an interface. For us, the focus is on the writing of interface functions.Introduction to how php calls webservice application_PHP tutorial

2.3. Use ASP.NET to call Web Service

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


Select the storage location and language and click OK to enter the default page. Then add the Web reference first and introduce the WebService into the current project. The method is: right-click in the resource manager, select Add Web Reference, and bring up the dialog box:

Introduction to how php calls webservice application_PHP tutorial

Fill in the URL, the address displayed on the browser after the WebService is run (i.e.: the access address after the WebService is released), click the "Go" button, as shown above, the referenced WebService will be displayed. method that can be called, and then click "Add Reference" to reference the webservice into the current project, as shown below, the imported WebService file will appear in the solution

Introduction to how php calls webservice application_PHP tutorial

Here we will practice the four methods of calling webservice and make a simple call example. First, add a few controls to the front desk of the website. The code is as follows:

Introduction to how php calls webservice application_PHP tutorial

Copy code

The code is as follows:

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>  
  
  
  
    无标题页  
  
  
   
  
   
  
          
               
               
          
               
   
  
   
  
  
 
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>



    无标题页


   

   

       
            
            
       
            
   

   



Then write the calling code in the background. Before calling, just like using other objects, you must instantiate it first. The instantiation method is localhost.Service a = new localhost.Service(); and then you can access the WebService through a There are methods provided inside. In this example, a button control is dynamically created to trigger the WebService call. The background code is as follows:
You can see the effect after running, as shown in the figure below. Enter two operands in the first two Textboxes. Select the operator from the middle drop-down list, and then click the "=" sign to output the calculated results to the third Textbox.

Introduction to how php calls webservice application_PHP tutorial

The entire calculation is not performed locally. It is calculated on the Web server and the result is returned to the caller through XML. Therefore, when running the program, the WebService program must also be started, otherwise it will An exception is reported that the remote server cannot be connected, as shown below:

Introduction to how php calls webservice application_PHP tutorial

The background code is as follows:

Copy code The 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; System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq; {
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("+"))
{ Text),double.Parse(Num2.Text))).ToString(); .subtract(double.Parse(Num1.Text), double.Parse(Num2.Text))).ToString();                                                            
        Result.Text = (web.multiplication(double.Parse(Num1.Text), double.Parse(Num2.Text))).ToString();                                                     "/"))                                                                         🎜> }
}




Copy code

The 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();
        }
    }
}

 

到此一个一个简单的WebService的开发和调用就已经完成了,在实际应用中可以根据自己的需要,写一些功能强大的,复杂的WebService,不管多么复杂,整个流程都是这样的。

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/326273.htmlTechArticle1.1、Web Service基本概念 Web Service也叫XML Web Service WebService是一种可以接收从Internet或者Intranet上的其它系统中传递过来的请求,轻量级的独立...
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!