web soap服务_使用PHP和SOAP创建Web服务,第2部分

web soap服务

In the first part of this series, I showed you how developing applications with the SOAP protocol is a great way to build interoperable software. I also demonstrated how easy it is to build your very own SOAP server and client using the NuSOAP library. This time around I’d like to introduce you to something that you will most definitely run into when working with SOAP – WSDL files.

本系列第一部分中 ,我向您展示了如何使用SOAP协议开发应用程序是构建可互操作软件的好方法。 我还演示了使用NuSOAP库构建自己的SOAP服务器和客户端是多么容易。 这次我想向您介绍一些在使用SOAP – WSDL文件时肯定会遇到的问题。

In this article we’ll talk about what WSDL files are and how to use them. I’ll show you how to quickly build your WSDL files with NuSOAP and incorporate a WSDL file into the SOAP server and client examples from the first part.

在本文中,我们将讨论什么是WSDL文件以及如何使用它们。 我将向您展示如何使用NuSOAP快速构建WSDL文件,以及如何从第一部分将WSDL文件合并到SOAP服务器和客户端示例中。

什么是WSDL文件? (What are WSDL Files?)

Web Services Description Language (WSDL) files are XML documents that provide metadata for a SOAP service. They contain information about the functions or methods the application makes available and what arguments to use. By making WSDL files available to the consumers of your service, it gives them the definitions they need to send valid requests precisely how you intend them to be. You can think of WSDL files as a complete contract for the application’s communication. If you truly want to make it easy for others to consume your service you will want to incorporate WSDL into your SOAP programming.

Web服务描述语言(WSDL)文件是XML文档,为SOAP服务提供元数据。 它们包含有关应用程序可用的功能或方法以及要使用的参数的信息。 通过使WSDL文件对您的服务使用者可用,它为他们提供了发送有效请求所需的定义,以准确地按照您的期望发送有效请求。 您可以将WSDL文件视为应用程序通信的完整合同。 如果您确实想让其他人轻松使用您的服务,则需要将WSDL合并到SOAP编程中。

WSDL结构 (WSDL Structure)

Just like SOAP messages, WSDL files have a specific schema to adhere to, and specific elements that must be in place to be valid. Let’s look at the major elements that make up a valid WSDL file and explain their uses.

就像SOAP消息一样,WSDL文件具有要遵循的特定架构以及必须放置的特定元素才有效。 让我们看一下构成有效WSDL文件的主要元素,并说明它们的用法。

<definitions>
 <types>
  ........
 </types>
 <message>
  <part></part>
 </message>
 <portType>
  .......
 </portType>
 <binding>
  ....
 </binding>
 <service>
  ....
 </service>
</definitions>

The root element of the WSDL file is the definitions element. This makes sense, as a WSDL file is by definition a definition of the web service. The types element describes the type of data used, which in the case of WSDL, XML schema is used. Within the messages element, is the definition of the data elements for the service. Each messages element can contain one or more part elements. The portType element defines the operations that can be performed with your web service and the request response messages that are used. Within the binding element, contains the protocol and data format specification for a particular portType. Finally, we have the service element which defines a collection of service element contains the URI (location) of the service.

WSDL文件的根元素是definitions元素。 这是有道理的,因为根据定义WSDL文件是Web服务的定义。 types元素描述了所使用的数据类型,在WSDL中,使用XML模式。 在messages元素中,是服务数据元素的定义。 每个messages元素可以包含一个或多个part元素。 portType元素定义可以通过Web服务执行的操作以及所使用的请求响应消息。 在binding元素内,包含特定portType的协议和数据格式规范。 最后,我们有service元素,它定义了 服务元素包含服务的URI(位置)。

The terminology has changed slightly in naming some of the elements in the WSDL 2.0 specification. portType, for example, has changed its name to Interface. Since support for WSDL 2.0 is weak, I’ve chosen to go over version 1.1 which is more widely used.

在命名WSDL 2.0规范中的某些元素时,术语已略有更改。 例如, portType已将其名称更改为Interface 。 由于对WSDL 2.0的支持很弱,因此我选择了使用范围更广的1.1版。

构建一个WSDL文件 (Building a WSDL File)

WSDL files can be cumbersome to write by hand as they must contain specific tags and are usually quite long. The nice thing about using NuSOAP is that it can create a WSDL file for you! Let’s modify the SOAP server we made in the first article to accommodate this.

WSDL文件手工编写可能很麻烦,因为它们必须包含特定的标签并且通常很长。 使用NuSOAP的好处是它可以为您创建WSDL文件! 让我们修改在第一篇文章中制作的SOAP服务器以适应这一点。

Open productlist.php and change it to reflect the code below:

打开productlist.php并进行更改以反映以下代码:

<?php
require_once "nusoap.php";

function getProd($category) {
    if ($category == "books") {
        return join(",", array(
            "The WordPress Anthology",
            "PHP Master: Write Cutting Edge Code",
            "Build Your Own Website the Right Way"));
    }
    else {
        return "No products listed under that category";
    }
}

$server = new soap_server();
$server->configureWSDL("productlist", "urn:productlist");

$server->register("getProd",
    array("category" => "xsd:string"),
    array("return" => "xsd:string"),
    "urn:productlist",
    "urn:productlist#getProd",
    "rpc",
    "encoded",
    "Get a listing of products by category");

$server->service($HTTP_RAW_POST_DATA);

Basically this is the same code as before but with only a couple of changes. The first change adds a call to configureWSDL(); the method acts as a flag to tell the server to generate a WSDL file for our service. The first argument is the name of the service and the second is the namespace for our service. A discussion of namespaces is really outside the scope of this article, but be aware that although we are not taking advantage of them here, platforms like Apache Axis and .NET do. It’s best to include them to be fully interoperable.

基本上,这是与以前相同的代码,但只有几处更改。 第一个更改添加了对configureWSDL()的调用; 该方法充当标志,告诉服务器为我们的服务生成WSDL文件。 第一个参数是服务的名称,第二个参数是我们服务的名称空间。 关于名称空间的讨论实际上不在本文讨论范围之内,但是请注意,尽管我们在这里没有利用它们,但Apache Axis和.NET之类的平台却可以使用。 最好包括它们以实现完全互操作。

The second change adds additional arguments to the register() method. Breaking it down:

第二个更改将其他参数添加到register()方法。 分解:

  • getProd is the function name

    getProd是函数名称

  • array("category" => "xsd:string") defines the input argument to getProd and its data type

    array("category" => "xsd:string")定义getProd的输入参数及其数据类型

  • array("return" => "xsd:string") defines the function’s return value and its data type

    array("return" => "xsd:string")定义函数的返回值及其数据类型

  • urn:productlist defines the namespace

    urn:productlist定义名称空间

  • urn:productlist#getProd defines the SOAP action

    urn:productlist#getProd定义SOAP操作

  • rpc defines the type of call (this could be either rpc or document)

    rpc定义调用的类型(可以是rpcdocument )

  • encoded defines the value for the use attribute (encoded or literal could be used)

    encoded定义了use属性的值(可以使用encodedliteral )

  • The last parameter is a documentation string that describes what the getProd function does

    最后一个参数是描述getProd函数做什么的文档字符串。

Now point your browser to http://yourwebroot/productlist.php?wsdl and you’ll see the brand new WSDL file created for you. Go ahead and copy that source and save it as it’s own file called products.wsdl and place it in you web directory.

现在,将浏览器指向http://yourwebroot/productlist.php?wsdl ,您将看到为您创建的全新WSDL文件。 继续复制该源并将其保存为自己的名为products.wsdl的文件,并将其放置在您的Web目录中。

与客户端一起使用WSDL文件 (Consuming WSDL Files with the Client)

We’ve modified the SOAP server to generate a WSDL file, so now lets modify the SOAP client to consume it. Open up productlistclient.php created in the previous article and simply change the line that initiates the client from this:

我们已经修改了SOAP服务器以生成WSDL文件,因此现在让我们修改SOAP客户端以使用它。 打开在上一篇文章中创建的productlistclient.php ,然后简单地从中更改启动客户端的行:

$client = new nusoap_client("http://localhost/nusoap/productlist.php");

to this:

对此:

$client = new nusoap_client("products.wsdl", true);

The second parameter in the nusoap_client() constructor call tells NuSOAP to build a SOAP client to accept the WSDL file. Now launch productlistclient.php in your browser and you should see the same result as before, but now you’re using WSDL power!

nusoap_client()构造函数调用中的第二个参数告诉NuSOAP构建SOAP客户端以接受WSDL文件。 现在,在浏览器中启动productlistclient.php ,您应该会看到与以前相同的结果,但是现在您正在使用WSDL功能!

摘要 (Summary)

In part 2 of this series on creating web services with PHP and SOAP, we went over the importance of using WSDL files for optimum interoperability. We talked about the different elements that make up a WSDL file and their definitions, and then I showed you how to quickly and easily create your own WSDL files with the NuSOAP library. Finally, we modified our SOAP server and client to demonstrate how to use WSDL in your applications.

在有关使用PHP和SOAP创建Web服务的系列文章的第2部分中,我们介绍了使用WSDL文件以实现最佳互操作性的重要性。 我们讨论了组成WSDL文件的不同元素及其定义,然后向您展示了如何使用NuSOAP库快速轻松地创建自己的WSDL文件。 最后,我们修改了SOAP服务器和客户端,以演示如何在您的应用程序中使用WSDL。

As you can probably guess, I’ve just barely scraped the surface of what SOAP can do for you, but with these new tools you can provide an easy and well-accepted way of exposing web services to your users.

您可能会猜到,我只是勉强了解SOAP可以为您做什么的表面,但是使用这些新工具,您可以提供一种简单且广为接受的向用户公开Web服务的方式。

Comments on this article are closed. Have a question about PHP? Why not ask it on our forums?

本文的评论已关闭。 对PHP有疑问吗? 为什么不在我们的论坛上提问呢?

Image via Lilyana Vynogradova / Shutterstock

图片来自Lilyana Vynogradova / Shutterstock

翻译自: https://www.sitepoint.com/web-services-with-php-and-soap-2/

web soap服务

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值