概述
本教程的目的是详细讨论如何使用Adobe创建一个 NuSOAP/PHP/SOAP web服务。 本教程对于初学者和专家都很理想。
我曾经写过一篇文章,讨论了如何使用NuSOAP开发 soap/php Web服务。 这是一个非常通用的教程,并没有涉及到实际编写第一个web服务的详细内容。 本教程使用了nichol网站的一些代码。
前提条件
- 本教程使用 php/C# 教程从原始开发 SOAP Web服务 中引用文本。代码和想法。 我强烈推荐你阅读这篇教程之前阅读。
- 我使用 ,但是你可以使用任意简单的或者像你喜欢的复杂的( 例如Microsoft )。
- 本文假设基本的PHP理解。
- PHP NuSOAP -的SOAP工具包
- Internet信息服务( IIS ) 或者带有PHP的等效web服务器
- 附加到本教程的C# 代码基于. NET 框架 2.0. .NET 框架的更高版本可能无法工作。
介绍
web服务为我们提供了在客户端/服务器配置之间进行通信的方法。 web服务实际上只是一组应用程序编程接口( api ),我们可以使用它来交换数据( 通常在网上)。 SOAP [XML derivative] 是通常用于跨所有平台和技术提供标准化的协议。
of是第三方插件,它将这个功能带到PHP中,所有的腿工作都为我们。
NuSOAP简介
NuSOAP提供了我们创建web服务所需的所有代码。
NuSOAP支持以下功能:
- 独立运行。不需要任何附加的Plug-Ins 或者服务器重新配置
- SOAP版本 1.1
- WSDL ( Web服务描述符语言) 1.1
- HTTP
- 复杂类型
确保已经下载并提取了NuSOAP并准备好进入它。
你好,世界服务示例
学习的最快,最简单的方法是做,所以让我们直接进入它。
require_once("nuSOAP/lib/nusoap.php");//Create a new soap server$server = new soap_server();//Define our namespace$namespace = "http://localhost/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文档:
<definitionsxmlns: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=http://localhost/soap/HelloWorld xmlns:soap=http://schemas.xmlsoap.org/wsdl/soap/xmlns:wsdl=http://schemas.xmlsoap.org/wsdl/xmlns=http://schemas.xmlsoap.org/wsdl/targetnamespace="http://localhost/soap/HelloWorld"><types><xsd:schematargetnamespace="http://localhost/soap/HelloWorld"><xsd:importnamespace="http://schemas.xmlsoap.org/soap/encoding/"><xsd:importnamespace="http://schemas.xmlsoap.org/wsdl/"></xsd:import></xsd:import></xsd:schema></types><messagename="HelloWorldRequest"><messagename="HelloWorldResponse"><porttypename="HelloWorldPortType"><operationname="HelloWorld"><inputmessage="tns:HelloWorldRequest"/><outputmessage="tns:HelloWorldResponse"></output></operation></porttype><bindingname="HelloWorldBinding"type="tns:HelloWorldPortType"><soap:bindingtransport="http://schemas.xmlsoap.org/soap/http"><operationname="HelloWorld"><soap:operationsoapaction="http://localhost/nusoaphelloworld/index.php/HelloWorld"><input/><soap:bodyuse="encoded"encodingstyle="http://schemas.xmlsoap.org/soap/encoding/"><output><soap:bodyuse="encoded"encodingstyle="http://schemas.xmlsoap.org/soap/encoding/"/></output></soap:body></soap:operation></operation></soap:binding></binding><servicename="HelloWorld"><portname="HelloWorldPort"binding="tns:HelloWorldBinding"><soap:addresslocation="http://localhost/nusoaphelloworld/index.php"></soap:address></port></service></message></message></definitions>
这里有一个很重要的问题,并且幸运的是,我们不需要关心自己的大多数。 比如,使用可视化工具时,解释器会自动读取WSDL文档,解释公开的方法。数据类型。类型。名称以及所有其他的东西,这样我们就不需要做什么了。
通过在地址栏的PHP文件 NAME的结尾键入 in,可以轻松地查看WSDL文档。 ( 比如。http://localhost/nusoaphelloworld/index.php?WSDL )
较少的示例
上例子很简单,但是你可以在这里做更多的事情。 实际上,你可以将值传递给方法以使它们更加有用。 例如,假设你希望将一个人的'姓名'传递给你的函数,然后将它的输出到屏幕。 这很容易通过向函数添加参数来实现。 例如:
require_once("nuSOAP/lib/nusoap.php");$namespace = "http://localhost/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 methodfunction HelloWorld($name)
{return"Hello". $name;
}//Our complex methodfunction 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();
注意,在我们 register的方法中,我们可以定义一些提供更多信息的附加参数。 我们可以为参数( 作为一个 array )。返回数据。甚至文档文本指定一个 List 来应用。
如果你在网络浏览器中查看你的网络服务,你将看到如下内容:
现在我们了解了如何将简单数据类型(。string
,integer
,等等 ) 传递给web服务,以及如何返回简单数据类型。 好,soap/xml Web服务 都用来定义复杂的自定义数据类型,以更有用。更结构化的方式返回数据。
复杂类型
复杂类型都用于创建我们自己的自定义数据类型,以便更轻松,更结构化的数据处理。 简单类型( 例如 string
和 integer
) 只有有限的用法。 如果我们可以创建自己的类型? 你可以使用NuSOAP轻松快捷地完成这一
创建复杂类型;
使用 AddComplexType
方法生成的nusoap来创建你自己的复杂类型:
//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')));
这个方法很简单。 给你复杂类型的NAME ( MyComplexType
),告诉你的WSDL文档,它是一个复杂的类型( struct
),然后指定。
在上面的示例中,我们的类型中有两个属性:ID
( integer
) 和 YourName
( string
)。
然后我们必须使用指定为数据类型而不是简单类型的复杂类型来 register 方法。 ( 我们在这里使用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服务。 这里示例在 Microsoft Windows Vista上使用 Visual C# 2010.
注意:本教程使用可视化 C# 2010,但同时使用了. NET 框架 2.0.
你的代码将类似于下面这样:
var ex = new HelloExample();string simpleResult = ex.HelloWorld("Jon");var myComplexType = new MyComplexType {ID = 1, YourName = "Jon"};
MyComplexType complexResult = ex.HelloComplexWorld(myComplexType);//OutputConsole.WriteLine("Simple: {0}", simpleResult);
Console.WriteLine("Complex: {0}", complexResult.YourName);
textBox1.Text = simpleResult;
textBox2.Text = complexResult.YourName;
你可以在演示应用程序中找到这里代码和更多内容。
现场演示
本教程讨论的网络服务是实时的,可以随时查看/使用:
历史记录
- 27th 2010年12月: 初始帖子