`
tiandirensoon
  • 浏览: 598284 次
文章分类
社区版块
存档分类
最新评论

php使用NuSoap产生webservice结合WSDL让asp.net调用

 
阅读更多


asp.net调用


=================================

参考:

使用NuSOAP结合WSDL来编程
类别:PHP 评论:0 浏览:513 发表时间:2009-09-10 16:59:38
From:http://www.scottnichol.com/nusoapprogwsdl.htm

这篇文章是接着 Introduction to NuSOAP, Programming with NuSOAPProgramming with NuSOAP Part 2 这三篇,增加了一些实例来说明如何使用 NuSOAP 结合 WSDL 来创建和使用 SOAP web service。

Hello, World Redux
The New Client
Defining New Data Structures

Hello, World Redux

我在 Introduction to NuSOAP 使用普遍的 “Hello,World” 实例,在那篇文章中,我演示了客户端和服务器端的请求和响应的交互,这里,我将使用 WSDL 来扩展那个实例。

WSDL 文件为 service 提供了 metadata,NuSOAP 允许程序员指定使用 soap_server 类的附加字段和方法的 service 创建的 WSDL。

Service 的代码必须依照产生的正确的 WSDL 的顺序做很多事情。service 的信息通过调用 configureWSDL 方法来指定,每个方法的信息也通过提供 register 方法的附加参数来指定,使用 WSDL 的 service 代码在下面的实例中演示:
<?php
// Pull in the NuSOAP code
require_once('nusoap.php');
// Create the server instance
$server = new soap_server();
// Initialize WSDL support
$server->configureWSDL('hellowsdl', 'urn:hellowsdl');
// Register the method to expose
$server->register('hello', // method name
array('name' => 'xsd:string'), // input parameters
array('return' => 'xsd:string'), // output parameters
'urn:hellowsdl', // namespace
'urn:hellowsdl#hello', // soapaction
'rpc', // style
'encoded', // use
'Says hello to the caller' // documentation
);
// Define the method as a PHP function
function hello($name) {
return 'Hello, ' . $name;
}
// Use the request to (try to) invoke the service
$HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';
$server->service($HTTP_RAW_POST_DATA);
?>

现在有些魔幻了,在你的浏览器上打开 service 的地址,在我的环境上是 http://localhost/phphack/hellowsdl.php,页面返回的内容提供了可以查看 service 的 WSDL 或者 查看每个方法信息的链接,这个实例是 hello 方法,页面显示的内容类似下图:

因此,只需要在 service 中加入很少的代码,NuSOAP 就可以提供 service 的阅读文档,但是那不是全部。在页面单击每一个 WSDL 链接或者在 URL 后加上 “?wsdl” 字符串,你就可以看到如下的 WSDL :
<?xml version="1.0"?>
<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:si="http://soapinterop.org/xsd"
xmlns:tns="urn:hellowsdl"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns="http://schemas.xmlsoap.org/wsdl/"
targetNamespace="urn:hellowsdl">
<types>
<xsd:schema targetNamespace="urn:hellowsdl">
<xsd:import namespace="http://schemas.xmlsoap.org/soap/encoding/" />
<xsd:import namespace="http://schemas.xmlsoap.org/wsdl/" />
</xsd:schema>
</types>
<message name="helloRequest">
<part name="name" type="xsd:string" />
</message>
<message name="helloResponse">
<part name="return" type="xsd:string" />
</message>
<portType name="hellowsdlPortType">
<operation name="hello">
<documentation>Says hello to the caller</documentation>
<input message="tns:helloRequest"/>
<output message="tns:helloResponse"/>
</operation>
</portType>
<binding name="hellowsdlBinding" type="tns:hellowsdlPortType">
<soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
<operation name="hello">
<soap:operation soapAction="urn:hellowsdl#hello" style="rpc"/>
<input>
<soap:body use="encoded" namespace="urn:hellowsdl"
encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
</input>
<output>
<soap:body use="encoded" namespace="urn:hellowsdl"
encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
</output>
</operation>
</binding>
<service name="hellowsdl">
<port name="hellowsdlPort" binding="tns:hellowsdlBinding">
<soap:address location="http://localhost/phphack/hellowsdl.php"/>
</port>
</service>
</definitions>

The New Client

在 service 中加入一些 NuSOAP WSDL 调用让它产生 WSDL 和其它的文档。相比之下,支持 WSDL 的客户端是突减的(anti-climactic),是少在这个简单的例子是。下面这个简单的例子和之前没有 WSDL 的客户端代码没有什么不同,唯一的不同是 soapclient 类的构造函数提供了一个 WSDL 的 URL 作为参数,而不是service 的地址。
<?php
// Pull in the NuSOAP code
require_once('nusoap.php');
// Create the client instance
$client = new soapclient('http://localhost/phphack/hellowsdl.php?wsdl', true);
// Check for an error
$err = $client->getError();
if ($err) {
// Display the error
echo '<h2>Constructor error</h2><pre>' . $err . '</pre>';
// At this point, you know the call that follows will fail
}
// Call the SOAP method
$result = $client->call('hello', array('name' => 'Scott'));
// Check for a fault
if ($client->fault) {
echo '<h2>Fault</h2><pre>';
print_r($result);
echo '</pre>';
} else {
// Check for errors
$err = $client->getError();
if ($err) {
// Display the error
echo '<h2>Error</h2><pre>' . $err . '</pre>';
} else {
// Display the result
echo '<h2>Result</h2><pre>';
print_r($result);
echo '</pre>';
}
}
// Display the request and response
echo '<h2>Request</h2>';
echo '<pre>' . htmlspecialchars($client->request, ENT_QUOTES) . '</pre>';
echo '<h2>Response</h2>';
echo '<pre>' . htmlspecialchars($client->response, ENT_QUOTES) . '</pre>';
// Display the debug messages
echo '<h2>Debug</h2>';
echo '<pre>' . htmlspecialchars($client->debug_str, ENT_QUOTES) . '</pre>';
?>

这里是 WSDL 实现的请求和响应信息:
POST /phphack/hellowsdl.php HTTP/1.0
Host: localhost
User-Agent: NuSOAP/0.6.8 (1.81)
Content-Type: text/xml; charset=ISO-8859-1
SOAPAction: &quoturn:hellowsdl#hello"
Content-Length: 550

<?xml version="1.0" encoding="ISO-8859-1"?>
<SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
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:si="http://soapinterop.org/xsd"
xmlns:tns="urn:hellowsdl">
<SOAP-ENV:Body>
<tns:hello xmlns:tns="urn:hellowsdl">
<name xsi:type="xsd:string">Scott</name>
</tns:hello>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

HTTP/1.1 200 OK
Server: Microsoft-IIS/5.0
Date: Wed, 03 Nov 2004 21:05:34 GMT
X-Powered-By: ASP.NET
X-Powered-By: PHP/4.3.4
Server: NuSOAP Server v0.6.8
X-SOAP-Server: NuSOAP/0.6.8 (1.81)
Content-Type: text/xml; charset=ISO-8859-1
Content-Length: 551

<?xml version="1.0" encoding="ISO-8859-1"?>
<SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
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:si="http://soapinterop.org/xsd">
<SOAP-ENV:Body>
<ns1:helloResponse xmlns:ns1="urn:hellowsdl">
<return xsi:type="xsd:string">Hello, Scott</return>
</helloResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

Defining New Data Structures

WSDL 一个重要的方面是它封装了一个或多个 XML 结构,允许程序员通过 service 来描述数据结构,为了说明 NuSOAP 如何支持这个,我会在 Programming with NuSOAP Part 2 文章中的 SOAP struct 实例中加入 WSDL 代码。

service 代码的改变已经显示在 Hello, World 实例中,但是它也包含了定义 Person 数据结构的代码:

<?php
// Pull in the NuSOAP code
require_once('nusoap.php');
// Create the server instance
$server = new soap_server();
// Initialize WSDL support
$server->configureWSDL('hellowsdl2', 'urn:hellowsdl2');
// Register the data structures used by the service
$server->wsdl->addComplexType(
'Person',
'complexType',
'struct',
'all',
'',
array(
'firstname' => array('name' => 'firstname', 'type' => 'xsd:string'),
'age' => array('name' => 'age', 'type' => 'xsd:int'),
'gender' => array('name' => 'gender', 'type' => 'xsd:string')
)
);
$server->wsdl->addComplexType(
'SweepstakesGreeting',
'complexType',
'struct',
'all',
'',
array(
'greeting' => array('name' => 'greeting', 'type' => 'xsd:string'),
'winner' => array('name' => 'winner', 'type' => 'xsd:boolean')
)
);
// Register the method to expose
$server->register('hello', // method name
array('person' => 'tns:Person'), // input parameters
array('return' => 'tns:SweepstakesGreeting'), // output parameters
'urn:hellowsdl2', // namespace
'urn:hellowsdl2#hello', // soapaction
'rpc', // style
'encoded', // use
'Greet a person entering the sweepstakes' // documentation
);
// Define the method as a PHP function
function hello($person) {
$greeting = 'Hello, ' . $person['firstname'] .
'. It is nice to meet a ' . $person['age'] .
' year old ' . $person['gender'] . '.';

$winner = $person['firstname'] == 'Scott';

return array(
'greeting' => $greeting,
'winner' => $winner
);
}
// Use the request to (try to) invoke the service
$HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';
$server->service($HTTP_RAW_POST_DATA);
?>

除了支持 WSDL 的附加代码之外,service 方法的代码本身也有一点改变,使用 WSDL ,不再需要使用 soapval 对象来为返回值指定名称和数据类型。
相似的, WSDL 客户端不需要使用 soapval 指定参数的名称和数据类型,演示代码如下:
<?php
// Pull in the NuSOAP code
require_once('nusoap.php');
// Create the client instance
$client = new soapclient('http://localhost/phphack/hellowsdl2.php?wsdl', true);
// Check for an error
$err = $client->getError();
if ($err) {
// Display the error
echo '<h2>Constructor error</h2><pre>' . $err . '</pre>';
// At this point, you know the call that follows will fail
}
// Call the SOAP method
$person = array('firstname' => 'Willi', 'age' => 22, 'gender' => 'male');
$result = $client->call('hello', array('person' => $person));
// Check for a fault
if ($client->fault) {
echo '<h2>Fault</h2><pre>';
print_r($result);
echo '</pre>';
} else {
// Check for errors
$err = $client->getError();
if ($err) {
// Display the error
echo '<h2>Error</h2><pre>' . $err . '</pre>';
} else {
// Display the result
echo '<h2>Result</h2><pre>';
print_r($result);
echo '</pre>';
}
}
// Display the request and response
echo '<h2>Request</h2>';
echo '<pre>' . htmlspecialchars($client->request, ENT_QUOTES) . '</pre>';
echo '<h2>Response</h2>';
echo '<pre>' . htmlspecialchars($client->response, ENT_QUOTES) . '</pre>';
// Display the debug messages
echo '<h2>Debug</h2>';
echo '<pre>' . htmlspecialchars($client->debug_str, ENT_QUOTES) . '</pre>';
?>

WSDL 是客户端多于一个功能,使用代理而不是用 soapclinet 类的 call 方法。代理(proxy)是一个类,它映射到 service 。因此,它具备了与 service 相同参数的相同方法,一些程序员更喜欢使用代理因为方法是作为用户一个实例的方法来调用的,而不是通过 call 方法,一个使用代理的实例如下:
<?php
// Pull in the NuSOAP code
require_once('nusoap.php');
// Create the client instance
$client = new soapclient('http://localhost/phphack/hellowsdl2.php?wsdl', true);
// Check for an error
$err = $client->getError();
if ($err) {
// Display the error
echo '<h2>Constructor error</h2><pre>' . $err . '</pre>';
// At this point, you know the call that follows will fail
}
// Create the proxy
$proxy = $client->getProxy();
// Call the SOAP method
$person = array('firstname' => 'Willi', 'age' => 22, 'gender' => 'male');
$result = $proxy->hello($person);
// Check for a fault
if ($proxy->fault) {
echo '<h2>Fault</h2><pre>';
print_r($result);
echo '</pre>';
} else {
// Check for errors
$err = $proxy->getError();
if ($err) {
// Display the error
echo '<h2>Error</h2><pre>' . $err . '</pre>';
} else {
// Display the result
echo '<h2>Result</h2><pre>';
print_r($result);
echo '</pre>';
}
}
// Display the request and response
echo '<h2>Request</h2>';
echo '<pre>' . htmlspecialchars($proxy->request, ENT_QUOTES) . '</pre>';
echo '<h2>Response</h2>';
echo '<pre>' . htmlspecialchars($proxy->response, ENT_QUOTES) . '</pre>';
// Display the debug messages
echo '<h2>Debug</h2>';
echo '<pre>' . htmlspecialchars($proxy->debug_str, ENT_QUOTES) . '</pre>';
?>

尽管可以使用常规的和代理的编码风格,但是请求和响应的信息是相同的。
POST /phphack/hellowsdl2.php HTTP/1.0
Host: localhost
User-Agent: NuSOAP/0.6.8 (1.81)
Content-Type: text/xml; charset=ISO-8859-1
SOAPAction: "urn:hellowsdl2#hello"
Content-Length: 676

<?xml version="1.0" encoding="ISO-8859-1"?>
<SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
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:si="http://soapinterop.org/xsd"
xmlns:tns="urn:hellowsdl2">
<SOAP-ENV:Body>
<tns:hello xmlns:tns="urn:hellowsdl2">
<person xsi:type="tns:Person">
<firstname xsi:type="xsd:string">Willi</firstname>
<age xsi:type="xsd:int">22</age>
<gender xsi:type="xsd:string">male</gender>
</person>
</tns:hello>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>


HTTP/1.1 200 OK
Server: Microsoft-IIS/5.0
Date: Wed, 03 Nov 2004 21:20:44 GMT
X-Powered-By: ASP.NET
X-Powered-By: PHP/4.3.4
Server: NuSOAP Server v0.6.8
X-SOAP-Server: NuSOAP/0.6.8 (1.81)
Content-Type: text/xml; charset=ISO-8859-1
Content-Length: 720

<?xml version="1.0" encoding="ISO-8859-1"?>
<SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
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:si="http://soapinterop.org/xsd"
xmlns:tns="urn:hellowsdl2">
<SOAP-ENV:Body>
<ns1:helloResponse xmlns:ns1="urn:hellowsdl2">
<return xsi:type="tns:SweepstakesGreeting">
<greeting xsi:type="xsd:string">
Hello, Willi. It is nice to meet a 22 year old male.
</greeting>
<winner xsi:type="xsd:boolean">0</winner>
</return>
</helloResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

分享到:
评论

相关推荐

    .net动态调用php的webservice例子[含源码]

    内含nusoap 写的一个简单php服务端,客户端采用.net写的一个动态调用类 相关帖子 http://topic.csdn.net/u/20090819/19/94f3754b-7635-4377-8dc4-08cab4cd1aad.html?87815 如果还有什么不清楚。可以联系我

    nusoap构建webservice (php)

    用php的soap构建webservice,很不错的类库

    Java调用php的webservice

    Java调用php的webservice: header("Content-Type:text/html;charset=UTF-8"); // require('lib/nusoap.php'); require_once("lib/nusoap.php"); $server = new soap_server(); $server-&gt;configureWSDL('...

    nusoap.php下载,下载nusoap.php

    nusoap.php下载

    php webservice类nusoap

    NULL 博文链接:https://feitu-jeff.iteye.com/blog/1902953

    nusoap-0.9.5及说明文档

    nusoap-0.9.5及说明文档 WSDL webservice

    PHP使用NuSOAP调用Web服务的方法

    本文实例讲述了PHP使用NuSOAP调用Web服务的方法。分享给大家供大家参考。具体如下: Steps: 1. Download nusoap library from internet. 2. Pass parameter list in your $client-&gt;call and enjoy. &lt;?php ...

    nusoap, 用于 PHP 5.6 7.2的固定 NuSOAP.zip

    nusoap, 用于 PHP 5.6 7.2的固定 NuSOAP PHP 5.4---7.1的为 PHP 5.4,5.5,5.6,7.0和 7.1 ( 已经测试) 修复的NuSOAP 。所有学分属于官方 author(s): http://nusoap.sourceforge.net 。

    PHP nusoap.php

    NULL 博文链接:https://zongyukai20070419095606.iteye.com/blog/321599

    PHP WebService实现

    本学习笔记是记录学习PHP WebService和WebService在SugarCRM上如何应用的过程及相关知识点摘录。 学习如何实现PHP WebService,php及java中如何调用。

    NUSOAP参考文档 PHP

    nusoap参考手册 ,HTML格式,学习参考用的。

    nusoap.php及若干实例

    包含nusoap.php所需库文件,以及24个实用例子

    nusoap.php

    利用:nusoap.php 可以用类的方式访问WEB SERVICE;对应BLOG文章可以参考:http://blog.csdn.net/xocom/article/details/52188799

    php 利用 nusoap 生成 webservices

    利用nusoap 生成webservices详细实例,保罗服务器端和客户端

    在PHP中利用wsdl创建标准webservice的实现代码

    网上有现成的nusoap,我没使用,如果使用了,我可能就不知道PHP是怎么创建webservice的了

    nusoap-0.7.2

    开源soap框架,可以用来php做手机接口,nusoap可以自动生成WSDL文档!

    nusoap-0.9.5.zip

    NuSOAP 是 PHP 环境下的 WEB 服务编程工具,用于创建或调用 WEB 服务。它是一个开源软件,当前版本是 0.9.5 ,支持 SOAP1.1 、 WSDL1.1 ,可以与其他支持 SOAP1.1 和 WSDL1.1 的系统互操作。 NuSOAP 完全由PHP语言...

    NuSOAP工具包

    NuSOAP是一组功能强大的PHP类,可以方便得创建和使用SOAP消息服务。为基于PHP开发Web Service提供了强大的开源支持。工具包中含源码和例子。使用nusoap只需引用nusoap.php即可,命令如下: include_once("lib/nusoap...

Global site tag (gtag.js) - Google Analytics