程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> php使用NuSoap產生webservice結合WSDL讓asp.net調用

php使用NuSoap產生webservice結合WSDL讓asp.net調用

編輯:關於PHP編程

 

  1. <?php     
  2. require_once("nusoap-0.9.5/lib/nusoap.php");     
  3. //定義服務程序         
  4. function Add($a,$b)  
  5. {  
  6.       return $a+$b;  
  7. }  
  8. //初始化服務對象 , 這個對象是類 soap_server 的一個實例      
  9. $soap = new soap_server;     
  10. //調用服務對象的 register 方法注冊需要被客戶端訪問的程序。     
  11. //只有注冊過的程序,才能被遠程客戶端訪問到。     
  12. $soap->configureWSDL('EventWSDL', 'http://tempuri.org/');  
  13. $soap->register('Add',  array("a"=>"xsd:string","b"=>"xsd:string"), // 輸入參數的定義     
  14. array("return"=>"xsd:string") // 返回參數的定義     
  15. );    
  16. //最後一步,把客戶端通過 post 方式提交的數據,傳遞給服務對象的 service 方法。      
  17. //service 方法處理輸入的數據,調用相應的函數或方法,並且生成正確的反饋,傳回給客戶端。     
  18. $HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';    
  19. $soap->service($HTTP_RAW_POST_DATA);    
  20. ?>   

\

asp.net調用

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


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

參考:

 

使用NuSOAP結合WSDL來編程 類別:PHP 評論:0 浏覽:513 發表  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>

 

作者 21aspnet

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved