php使用NuSoap产生webservice结合WSDL让asp.net调用
<?php
require_once("nusoap-0.9.5/lib/nusoap.php");
//定義服務(wù)程序
function Add($a,$b)
{return $a+$b;
}
//初始化服務(wù)對象 , 這個(gè)對象是類 soap_server 的一個(gè)實(shí)例
$soap = new soap_server;
//調(diào)用服務(wù)對象的 register 方法注冊需要被客戶端訪問的程序。
//只有注冊過的程序,才能被遠(yuǎn)程客戶端訪問到。
$soap->configureWSDL('EventWSDL', 'http://tempuri.org/');
$soap->register('Add', array("a"=>"xsd:string","b"=>"xsd:string"), // 輸入?yún)?shù)的定義
array("return"=>"xsd:string") // 返回參數(shù)的定義
);
//最后一步,把客戶端通過 post 方式提交的數(shù)據(jù),傳遞給服務(wù)對象的 service 方法。
//service 方法處理輸入的數(shù)據(jù),調(diào)用相應(yīng)的函數(shù)或方法,并且生成正確的反饋,傳回給客戶端。
$HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';
$soap->service($HTTP_RAW_POST_DATA);
?>
asp.net調(diào)用
lt.EventWSDL ew =new webserviceTest.lt.EventWSDL();Response.Write(ew.Add("1","7").ToString());
=================================
參考:
?
使用NuSOAP結(jié)合WSDL來編程 類別:PHP 評論:0 瀏覽:513 發(fā)表時(shí)間:2009-09-10 16:59:38From:http://www.scottnichol.com/nusoapprogwsdl.htm這篇文章是接著 Introduction to NuSOAP, Programming with NuSOAP 和 Programming with NuSOAP Part 2 這三篇,增加了一些實(shí)例來說明如何使用 NuSOAP 結(jié)合 WSDL 來創(chuàng)建和使用 SOAP web service。
?Hello, World Redux
The New Client
Defining New Data Structures
Hello, World Redux
我在 Introduction to NuSOAP 使用普遍的 “Hello,World” 實(shí)例,在那篇文章中,我演示了客戶端和服務(wù)器端的請求和響應(yīng)的交互,這里,我將使用 WSDL 來擴(kuò)展那個(gè)實(shí)例。?WSDL 文件為 service 提供了 metadata,NuSOAP 允許程序員指定使用 soap_server 類的附加字段和方法的 service 創(chuàng)建的 WSDL。
?Service 的代碼必須依照產(chǎn)生的正確的 WSDL 的順序做很多事情。service 的信息通過調(diào)用 configureWSDL 方法來指定,每個(gè)方法的信息也通過提供 register 方法的附加參數(shù)來指定,使用 WSDL 的 service 代碼在下面的實(shí)例中演示:
<?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);
?>
現(xiàn)在有些魔幻了,在你的瀏覽器上打開 service 的地址,在我的環(huán)境上是 http://localhost/phphack/hellowsdl.php,頁面返回的內(nèi)容提供了可以查看 service 的 WSDL 或者 查看每個(gè)方法信息的鏈接,這個(gè)實(shí)例是 hello 方法,頁面顯示的內(nèi)容類似下圖:
?
因此,只需要在 service 中加入很少的代碼,NuSOAP 就可以提供 service 的閱讀文檔,但是那不是全部。在頁面單擊每一個(gè) 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 調(diào)用讓它產(chǎn)生 WSDL 和其它的文檔。相比之下,支持 WSDL 的客戶端是突減的(anti-climactic),是少在這個(gè)簡單的例子是。下面這個(gè)簡單的例子和之前沒有 WSDL 的客戶端代碼沒有什么不同,唯一的不同是 soapclient 類的構(gòu)造函數(shù)提供了一個(gè) WSDL 的 URL 作為參數(shù),而不是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 實(shí)現(xiàn)的請求和響應(yīng)信息:?
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: "urn: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 一個(gè)重要的方面是它封裝了一個(gè)或多個(gè) XML 結(jié)構(gòu),允許程序員通過 service 來描述數(shù)據(jù)結(jié)構(gòu),為了說明 NuSOAP 如何支持這個(gè),我會(huì)在 Programming with NuSOAP Part 2 文章中的 SOAP struct 實(shí)例中加入 WSDL 代碼。service 代碼的改變已經(jīng)顯示在 Hello, World 實(shí)例中,但是它也包含了定義 Person 數(shù)據(jù)結(jié)構(gòu)的代碼:
<?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 方法的代碼本身也有一點(diǎn)改變,使用 WSDL ,不再需要使用 soapval 對象來為返回值指定名稱和數(shù)據(jù)類型。相似的, WSDL 客戶端不需要使用 soapval 指定參數(shù)的名稱和數(shù)據(jù)類型,演示代碼如下: <?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 是客戶端多于一個(gè)功能,使用代理而不是用 soapclinet 類的 call 方法。代理(proxy)是一個(gè)類,它映射到 service 。因此,它具備了與 service 相同參數(shù)的相同方法,一些程序員更喜歡使用代理因?yàn)榉椒ㄊ亲鳛橛脩粢粋€(gè)實(shí)例的方法來調(diào)用的,而不是通過 call 方法,一個(gè)使用代理的實(shí)例如下:
<?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>';
?>
盡管可以使用常規(guī)的和代理的編碼風(fēng)格,但是請求和響應(yīng)的信息是相同的。
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>
?
總結(jié)
以上是生活随笔為你收集整理的php使用NuSoap产生webservice结合WSDL让asp.net调用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 花生焖猪尾怎么做?
- 下一篇: apache日志分析简介