<?php
require_once("nusoap-0.9.5/lib/nusoap.php");
//定义服务程序
function Add($a,$b)
{
return $a+$b;
}
//初始化服务对象 , 这个对象是类 soap_server 的一个实例
$soap = new soap_server;
//调用服务对象的 register 方法注册需要被客户端访问的程序。
//只有注册过的程序,才能被远程客户端访问到。
$soap->configureWSDL('EventWSDL', 'http://tempuri.org/');
$soap->register('Add',  array("a"=>"xsd:string","b"=>"xsd:string"), // 输入参数的定义
array("return"=>"xsd:string") // 返回参数的定义
);
//最后一步,把客户端通过 post 方式提交的数据,传递给服务对象的 service 方法。
//service 方法处理输入的数据,调用相应的函数或方法,并且生成正确的反馈,传回给客户端。
$HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';
$soap->service($HTTP_RAW_POST_DATA);
?> 

asp.net调用

lt.EventWSDL ew =new webserviceTest.lt.EventWSDL();
Response.Write(ew.Add("1","7").ToString());

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

参考:

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

这篇文章是接着 Introduction to NuSOAP, Programming with NuSOAP 和 Programming 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>

php使用NuSoap产生webservice结合WSDL让asp.net调用相关推荐

  1. 浅淡Webservice、WSDL三种服务访问的方式(附案例)

    Webservice Webservice是使应用程序以与平台和编程语言无关的方式进行相互通信技术. eg:站点提供访问的数据接口:新浪微博.淘宝. 官方解释:它是一种构建应用程序的普遍模型,可以在任 ...

  2. [Java] webservice soap,wsdl 例子

    java 调用webservice的各种方法总结 现在webservice加xml技术已经逐渐成熟,但要真正要用起来还需时日!! 由于毕业设计缘故,我看了很多关于webservice方面的知识,今天和 ...

  3. CXF发布服务,WebService缺少wsdl:types/wsdl:message/标签

    发布CXF时遇到一个问题,发布的服务,wsdl文件中没有<wsdl:types/><wsdl:message/>标签,wsdl文件如下: <?xml version=&q ...

  4. axis2 webservice入门学识(JS,Java,PHP调用实例源码)

    来源:http://www.myexception.cn/web/952419.html axis2 webservice入门知识(JS,Java,PHP调用实例源码) 背景简介 最近接触到一个银行接 ...

  5. cmd 调用webservice接口_c# 三种方法调用WebService接口

    1.引用*.wsdl文件 WebService服务端会提供wsdl文件,客户端通过该文件生成.cs文件以及生成.dll. 注意:若服务端只提供的了URL,在URL后面加上"?wsdl&quo ...

  6. Java WebService 接口的简单生成和调用

    Java WebService 接口的简单生成和调用 1.打开eclipse创建普通的java项目,写一个类  如下: package com.jspweb.ws;import javax.jws.W ...

  7. webservice的POST和GET请求调用

    webservice的POST和GET请求调用 POST请求 1.发送请求 import java.io.DataOutputStream; import java.io.InputStream; i ...

  8. webservice发布问题,部署iis后调用不成功

    我使用的环境win8.vs2010.IIS8.0 vs2010中创建的webservice中的方法可以正常调用,但是发布到IIS上后,asmx文件能正常访问, 但是进入方法后, 点击 "调用 ...

  9. nusoap php 7,nusoap-用php的NuSoap 访问webservice遇到的问题。

    下了0.9.5版本的nusoap,访问java开发的webservice接口, 结果一直都连不上,帮忙看一下是什么问题.谢谢. 程序报错: Error wsdl error: XML error pa ...

最新文章

  1. jenkins pipeline脚本_Jenkins 创建流水线 (Pipeline) 项目的脚本
  2. Python中的eval(),exec()以及其相关函数
  3. 机器学习之无监督学习——聚类
  4. CometD:Java Web应用程序的Facebook类似聊天
  5. Kettle7.1在window启动报错
  6. cnpm不是内部或外部命令 cnpm: command not found 解决方案 cnpm
  7. C程序设计语言现代方法01:C语言概述
  8. 【ASP.NET】HTTP中的 get 和 post 请求
  9. SQLServer的本月统计和本周统计
  10. 一道有意思的概率应用题
  11. 多测师肖sir__金牌高级讲师软件测试___疑难杂症之app端口占用
  12. 智能指针是一种类,别名称为句柄类
  13. 蘑菇租房爆雷,房东围堵总部,CEO凌晨发公告
  14. 自学c语言要下载什么软件下载,你学c语言用的什么app?
  15. python红色的颜色表达式_python – 排序(十六进制)颜色以匹配彩虹
  16. 理解OAuth 2.0
  17. iptables禁止访问某个域名
  18. R语言股票市场指数:ARMA-GARCH模型和对数收益率数据探索性分析
  19. python wait notify_java与python多线程wait,notify操作比较
  20. 一、代理模式{proxy-pattern}

热门文章

  1. 流程图函数’怎么画_原来函数可以这么美!
  2. python编译环境搭建_python开发环境搭建
  3. 晴天计算机按键,电脑怎样才能在按键的时候,按下去按键没反应,手台起来才有呢?...
  4. 力扣(LeetCode)刷题,简单+中等题(第30期)
  5. 判断文件或文件夹(目录)是否存在 C/C++ win/linux通用
  6. linux支持hd610显卡吗,HD610相当于什么显卡 HD610和HD630的区别 (全文)
  7. C++中模板template typename T
  8. Udacity机器人软件工程师课程笔记(十四)-运动学-正向运动学和反向运动学(其一)
  9. VSCode用浏览器预览/运行html文件
  10. 一道题弄明白二维数组的指针