65.9K
CodeProject 正在变化。 阅读更多。
Home

PHP NuSOAP 教程

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.86/5 (10投票s)

2010 年 12 月 27 日

GPL3

5分钟阅读

viewsIcon

231164

downloadIcon

13452

面向初学者的基本 PHP NuSOAP 教程

概述

本教程的目的是详细讨论如何使用 Adobe Dreamweaver CS4 创建 NuSOAP/PHP/SOAP Web 服务。本教程适合初学者和专家。

我曾撰写过一篇关于如何使用 NuSOAP 开发 SOAP/PHP Web 服务的文章。那是一篇非常通用的教程,并没有详细介绍如何编写第一个 Web 服务。本教程使用了一些来自 Scott Nichol 网站的代码。

必备组件

  • 本教程引用了原始《使用 PHP/C# 开发 SOAP Web 服务》教程中的文本、代码和想法。强烈建议您在阅读本教程之前先阅读该教程。
  • 在本教程中,我使用的是 Adobe Dreamweaver CS4,但您也可以使用任何其他工具,从简单的(如记事本)到复杂的(如 Microsoft Expression Studio)都可以。
  • 本文档假设您已具备基本的 PHP 知识。
  • NuSOAP – PHP 的 SOAP 工具包
  • Internet Information Services (IIS) 或支持 PHP 的同等 Web 服务器
  • 本教程附带的 C# 代码基于 .NET Framework 2.0。更高版本的 .NET Framework 可能不兼容。

引言

Web 服务为我们提供了一种客户端/服务器配置之间的通信方式。Web 服务本质上是一组应用程序编程接口(API),我们可以使用它们来交换数据(通常通过 Web)。SOAP [XML 衍生] 是通常用于在所有平台和技术之间实现标准化的协议。

NuSOAP 是一个第三方插件,它为 PHP 提供了这种功能,并且已经完成了大部分繁重的工作。

NuSOAP 简介

NuSOAP 提供了创建 Web 服务所需的所有预先编写的代码。

NuSOAP 支持以下功能

  • 独立运行。无需任何额外的插件或服务器重新配置
  • SOAP 版本 1.1
  • WSDL(Web 服务描述语言)1.1
  • HTTP
  • 复杂类型

请确保您已下载并解压了 NuSOAP,并已准备好开始使用。

“Hello, World!” 示例 Web 服务

最快、最简单地学习方法就是动手实践,让我们立即开始。

require_once("nuSOAP/lib/nusoap.php");
 
//Create a new soap server
$server = new soap_server();
 
//Define our namespace
$namespace = "https:///nusoaphelloworld/index.php";
$server->wsdl->schemaTargetNamespace = $namespace;
 
//Configure our WSDL
$server->configureWSDL("HelloWorld");
 
// Register our method
$server->register('HelloWorld');
 
function HelloWorld()
{
return "Hello, World!";
}
 
// Get our posted data if the service is being consumed
// otherwise leave this data blank.
$POST_DATA = isset($GLOBALS['HTTP_RAW_POST_DATA']) ? $GLOBALS['HTTP_RAW_POST_DATA'] : '';
 
// pass our posted data (or nothing) to the soap service
$server->service($POST_DATA);
exit(); 

这与我最初编写的代码示例非常相似,但打好基础很重要。让我们看看这里发生了什么

  • 创建一个 soap_server
  • 定义 Web 服务器的命名空间并配置我们的 WSDL 文档
  • 注册我们的 HelloWorld 方法
  • 编写我们的实际方法
  • 输出原始数据

此 Web 服务公开了一个名为“HelloWorld”的单一方法,该方法在调用时会输出一个“Hello, World!string

NuSOAP 生成的 WSDL 文档如下所示

<definitions xmlns:soap-env=http://schemas.xmlsoap.org/soap/envelope/ 
	xmlns:xsd=http://www.w3.org/2001/XMLSchema 
	xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance 
	xmlns:soap-enc=http://schemas.xmlsoap.org/soap/encoding/ 
	xmlns:tns=https:///soap/HelloWorld 
	xmlns:soap=http://schemas.xmlsoap.org/wsdl/soap/ 
	xmlns:wsdl=http://schemas.xmlsoap.org/wsdl/ 
	xmlns=http://schemas.xmlsoap.org/wsdl/ 
	targetnamespace="https:///soap/HelloWorld">
<types>
<xsd:schema targetnamespace="https:///soap/HelloWorld">
<xsd:import namespace="http://schemas.xmlsoap.org/soap/encoding/">
<xsd:import namespace="http://schemas.xmlsoap.org/wsdl/">
</xsd:import></xsd:import></xsd:schema>
</types>
<message name="HelloWorldRequest">
<message name="HelloWorldResponse">
<porttype name="HelloWorldPortType">
<operation name="HelloWorld">
<input message="tns:HelloWorldRequest" />
<output message="tns:HelloWorldResponse">
</output></operation>
</porttype>
<binding name="HelloWorldBinding" type="tns:HelloWorldPortType">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http">
<operation name="HelloWorld">
<soap:operation soapaction="https:///nusoaphelloworld/index.php/HelloWorld">
<input /><soap:body use="encoded" 
	encodingstyle="http://schemas.xmlsoap.org/soap/encoding/">
<output><soap:body use="encoded" 
	encodingstyle="http://schemas.xmlsoap.org/soap/encoding/" /></output>
</soap:body></soap:operation></operation>
</soap:binding></binding>
<service name="HelloWorld">
<port name="HelloWorldPort" binding="tns:HelloWorldBinding">
<soap:address location="https:///nusoaphelloworld/index.php">
</soap:address></port>
</service>
</message></message></definitions> 

这里有很多内容,幸运的是,我们不必关心其中大部分。在使用客户端工具(例如 Visual C#)时,解释器会自动读取 WSDL 文档,解析公开的方法、数据类型、复杂类型、名称以及其他所有信息,因此我们实际上无需做任何事情。

您可以通过在地址栏的 PHP 文件名后附加 ?WSDL 来轻松查看 WSDL 文档。(例如: https:///nusoaphelloworld/index.php?WSDL

更复杂的示例

上面的示例很简单,但还有更多功能。您可以实际向方法传递值,使其更加有用。例如,假设您想将一个人的“名字”传递给函数,然后在屏幕上输出。通过向函数添加参数可以轻松实现这一点。示例

require_once("nuSOAP/lib/nusoap.php");
$namespace = "https:///nusoaphelloworld/index.php";
 
// create a new soap server
$server = new soap_server();
 
// configure our WSDL
$server->configureWSDL("HelloExample");
 
// set our namespace
$server->wsdl->schemaTargetNamespace = $namespace;
 
//Register a method that has parameters and return types
$server->register(
// method name:
'HelloWorld',
// parameter list:
array('name'=>'xsd:string'),
// return value(s):
array('return'=>'xsd:string'),
// namespace:
$namespace,
// soapaction: (use default)
false,
// style: rpc or document
'rpc',
// use: encoded or literal
'encoded',
// description: documentation for the method
'Simple Hello World Method');
 
//Create a complex type
$server->wsdl->addComplexType('MyComplexType','complexType','struct','all','',
array( 'ID' => array('name' => 'ID','type' => 'xsd:int'),
'YourName' => array('name' => 'YourName','type' => 'xsd:string')));
 
//Register our method using the complex type
$server->register(
// method name:
'HelloComplexWorld',
// parameter list:
array('name'=>'tns:MyComplexType'),
// return value(s):
array('return'=>'tns:MyComplexType'),
// namespace:
$namespace,
// soapaction: (use default)
false,
// style: rpc or document
'rpc',
// use: encoded or literal
'encoded',
// description: documentation for the method
'Complex Hello World Method');
 
//Our Simple method
function HelloWorld($name)
{
return "Hello " . $name;
}
 
//Our complex method
function HelloComplexWorld($mycomplextype)
{
return $mycomplextype;
}
 
// Get our posted data if the service is being consumed
// otherwise leave this data blank.
$POST_DATA = isset($GLOBALS['HTTP_RAW_POST_DATA']) ? $GLOBALS['HTTP_RAW_POST_DATA'] : '';
 
// pass our posted data (or nothing) to the soap service
$server->service($POST_DATA);
exit(); 

请注意,在注册方法时,我们可以定义一些额外的参数来提供更多信息。我们可以指定参数列表(作为数组)、返回数据,甚至要应用的文档文本。

如果您在 Web 浏览器中查看 Web 服务,您会看到类似这样的内容

Web Service - Click to enlarge image

现在我们了解了如何将简单数据类型(stringinteger 等)传递给 Web 服务以及如何返回简单数据类型,接下来是什么?嗯,SOAP/XML Web 服务都使用复杂自定义数据类型来以更有用、更结构化的方式返回数据。

复杂类型

复杂类型用于创建我们自己的自定义数据类型,以便更轻松、更结构化地处理数据。简单类型(如 stringinteger)的用途有限。如果我们能创建自己的类型怎么办?使用 NuSOAP,您可以轻松快捷地做到这一点;
创建您的复杂类型;

使用 NuSOAP 内置的 AddComplexType 方法创建您自己的复杂类型

//Create a complex type
$server->wsdl->addComplexType('MyComplexType','complexType','struct','all','',
array( 'ID' => array('name' => 'ID','type' => 'xsd:int'),
'YourName' => array('name' => 'YourName','type' => 'xsd:string'))); 

该方法非常简单。为您的复杂类型命名(MyComplexType),告知您的 WSDL 文档它是一个复杂类型(struct),然后指定参数。

在上面的示例中,我们的类型有两个属性:IDInteger)和 YourNamestring)。

然后,我们必须注册我们的方法,并将复杂类型指定为数据类型,而不是简单类型。(我们在此处使用 tns 而不是 xsd。)

//Register our method using the complex type
$server->register(
// method name:
'HelloComplexWorld',
// parameter list:
array('name'=>'tns:MyComplexType'),
// return value(s):
array('return'=>'tns:MyComplexType'),
// namespace:
$namespace,
// soapaction: (use default)
false,
// style: rpc or document
'rpc',
// use: encoded or literal
'encoded',
// description: documentation for the method
'Complex Hello World Method'); 

在这种情况下,我们指定了参数的类型为 MyComplexType,并且也返回 MyComplexType 数据类型。

消费

您可以从支持 Web 服务的任何平台/语言进行消费(使用、实现)Web 服务。本示例使用 Microsoft Windows Vista 上的 Visual C# 2010。

注意:本教程使用了 Visual C# 2010,但也使用了 .NET Framework 2.0。

在此处阅读有关 Visual C# 消费的完整教程。

您的代码将如下所示

var ex = new HelloExample();
string simpleResult = ex.HelloWorld("Jon");
 
var myComplexType = new MyComplexType {ID = 1, YourName = "Jon"};
MyComplexType complexResult = ex.HelloComplexWorld(myComplexType);
 
//Output
Console.WriteLine("Simple: {0}", simpleResult);
Console.WriteLine("Complex: {0}", complexResult.YourName);
 
textBox1.Text = simpleResult;
textBox2.Text = complexResult.YourName; 

您可以在演示应用程序中找到此代码以及更多代码。

实时演示

本教程中讨论的 Web 服务是实时的,可以随时查看/消费

历史

  • 2010 年 12 月 27 日:首次发布
© . All rights reserved.