2015-08-08 116 views
0

我打算使用PHP實現web服務。 Web服務的創建者通過電子郵件向我發送了需要的WSDL,並且我能夠將它們導入到Soap UI中並對其進行測試。PHP中的SOAP UI定義和實現

看到他們工作得很好,我的任務是在我的PHP應用程序中重複相同的過程。我做了一些搜索,發現PHP5已經提供了一個SoapClient來使用web服務。我甚至測試了兩個例子,他們工作得很好。但不是使用Soap UI運行的那個。

SoapClient接收WSDL文件的URI作爲第一個參數1 - 這是soapui在頂欄中顯示的服務的URL嗎?我注意到,我測試的其他web服務,如果uri被複制並粘貼到瀏覽器,XML格式將返回有關web服務的數據。在soapui數據被指向端點時,瀏覽器只會輸出「需要長度」411錯誤消息。

所以我的問題是,SOAP ui用於導入項目,我應該指向我的PHP的.xml文件?像:

SoapClient ("file:://C:\users\something\webservice.xml?wsdl", 
['service'=>'login', 'username'=>'something', 'password'=>'secret' ]); 

我會暴露與web服務信息收到的.xml,但我怕泄露敏感數據。我將複製請求的報頭,ommiting任何敏感數據

<?xml version="1.0" encoding="UTF-8"?> 
<con:soapui-project activeEnvironment="Default" name="" resourceRoot="" soapui-version="5.2.0" abortOnError="false" runType="SEQUENTIAL" id="" xmlns:con="..."><con:settings/><con:interface xsi:type="con:WsdlInterface" wsaVersion="NONE" name="..." type="wsdl" bindingName="{...}GenericTicketConnector_Service" soapVersion="1_1" anonymous="optional" definition="file:/D:/.../Documents/file.wsdl" id="..." xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><con:settings/><con:definitionCache type="TEXT" rootPart="file:\D:\...\Documents\file.wsdl"><con:part><con:url>file:\D:\...\Documents\file.wsdl</con:url><con:content><![CDATA[<--!...--> 
<wsdl:definitions name="GenericTicketConnector" targetNamespace="http://www.otrs.org/TicketConnector/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://www.otrs.org/TicketConnector/"> 
    <wsdl:documentation/> 
    <!--Warning: This WSDL file is for Development and Test purposes ONLY!--> 
    <wsdl:types> 
    <xsd:schema targetNamespace="http://www.otrs.org/TicketConnector/"> 

此.xml文件看起來像一個正常的WSDL文件後,描述所提供的web服務的請求,respose ...等格式...

謝謝。

回答

1

我對SoapUI並不熟悉,但對於SoapClient(第一個參數)的ctor的WSDL參數,如果您將它作爲HTTP URL或本地文件的路徑,則沒什麼區別。

請看下面的例子會下載就飛到哪裏示例腳本存儲在目錄中的WSDL文件,然後使用本地文件,而不是網址:

// just an example webservice WSDL 
$wsdl = 'http://www.webservicex.net/globalweather.asmx?WSDL'; 

// store the WSDL file in current directory if it does not yet exist 
$filename = __DIR__ . '/globalweather.asmx.wsdl'; 
if (!is_readable($filename)) { 
    file_put_contents($filename, fopen($wsdl, 'r')); 
} 

$soapclient = new SoapClient($filename); 
$params  = ['CountryName' => 'Spain', 'CityName' => 'Alicante']; 
$response = $soapclient->getWeather($params); 

var_dump($response); 

的響應是一個(未映射) stdClassGetWeatherResult從web服務。已經調用$soapclient->getWeather的調用只有在WSDL成功加載的情況下才會如此。

注意:在Windows操作系統上,問號(「?」)在文件名中無效,因此請注意只使用有效的文件名。

WSDL文件包含所有需要了解的與Web服務交互的數據。所以你不需要更多的信息。

... 
    <wsdl:service name="GlobalWeather"> 
    <wsdl:port name="GlobalWeatherSoap" binding="tns:GlobalWeatherSoap"> 
     <soap:address location="http://www.webservicex.net/globalweather.asmx" /> 
    </wsdl:port> 
... 

如本例所示,所述WSDL包含混凝土URI SoapClient的(或了SoapUI就此而言)將發送HTTP請求。如果它接受或不接受,請嘗試使用SoapUI中的本地文件。


編輯:WSDL file of the OTRS Websvervice is available on Github,這裏採用的OTRS web服務,剛剛上市的方法和類型的例子:

$wsdl = 'https://raw.githubusercontent.com/OTRS/otrs/master/development/webservices/GenericTicketConnectorSOAP.wsdl'; 

// store the WSDL file in current directory if it does not yet exist 
$filename = __DIR__ . '/GenericTicketConnectorSOAP.wsdl'; 
if (!is_readable($filename)) { 
    file_put_contents($filename, fopen($wsdl, 'r')); 
} 

$soapclient = new SoapClient($filename); 
print_r($soapclient->__getFunctions()); 
print_r($soapclient->__getTypes()); 

輸出:

Array 
(
    [0] => TicketCreateResponse TicketCreate(TicketCreate $parameters) 
    [1] => TicketUpdateResponse TicketUpdate(TicketUpdate $parameters) 
    [2] => TicketGetResponse TicketGet(TicketGet $parameters) 
    [3] => TicketSearchResponse TicketSearch(TicketSearch $parameters) 
    [4] => SessionCreateResponse SessionCreate(SessionCreate $parameters) 
) 
... 
+0

我試圖改變這個地址就是所謂的「肥皂文件」。像這樣:'$ client = new SoapClient(「file:/// C:/Users/nb21334/Desktop/OTRSTicketConnector-soapui-project.wsdl」,$ params);' - 但我得到錯誤'SOAP-ERROR:解析WSDL:在'file:/// C:/ Users(文件路徑的其餘部分)'中找不到' - 我嘗試使用.xml和.wsdl擴展名。 – Thaenor

+1

WSDL文件的聲音不完整。聯繫OTRS並詢問他們Webservice的是什麼以及WSDL文件中缺少元素的原因。他們可能有這個理由。根據缺少的內容,您還可以通過選項提供數據。 – hakre

+0

我想我沒有使用正確的文件。要麼我需要自己生成wsdl,要麼還有其他的東西丟失。有沒有一種方法可以手動編寫附加到請求的xml並手動將它發送到服務器?我得到的錯誤發生,因爲我沒有喂正確的wsdl到php。 (在PHP上,請求甚至不會被髮送,我知道這是因爲我完全相同的錯誤,無論是在線還是離線) – Thaenor