2012-01-30 52 views
0

這是我第一次與Web服務/ SOAP .NET Web服務......我一直在試圖消耗使用PHP,但無濟於事的.NET Web服務。我已經搜索並閱讀了谷歌拋出所有與此相關的所有頁面,但我仍然迷失。消費使用PHP

的東西是SOAP服務我試圖調用具有授權頭,我無法想出一個辦法來驗證我的要求。

我已經嘗試了PHP的SoapClient的和的NuSOAP,但都沒有可用的示例代碼,這將有助於。所以任何幫助都會很棒。

以下是一個示例SOAP 1.1請求和響應。

POST /OxiWalletService/Service.asmx HTTP/1.1 
Host: 172.160.0.49 
Content-Type: text/xml; charset=utf-8 
Content-Length: length 
SOAPAction: "http://tempuri.org/WS_GetData" 

<?xml version="1.0" encoding="utf-8"?> 
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> 
<soap:Header> 
    <AuthHeader xmlns="http://tempuri.org/"> 
    <UserName>string</UserName> 
    <Password>string</Password> 
    </AuthHeader> 
</soap:Header> 
<soap:Body> 
    <WS_GetData xmlns="http://tempuri.org/"> 
    <xmlString>string</xmlString> 
    </WS_GetData> 
</soap:Body> 
</soap:Envelope> 

響應

HTTP/1.1 200 OK 
Content-Type: text/xml; charset=utf-8 
Content-Length: length 

<?xml version="1.0" encoding="utf-8"?> 
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> 
<soap:Body> 
    <WS_GetDataResponse xmlns="http://tempuri.org/"> 
    <WS_GetDataResult>string</WS_GetDataResult> 
    </WS_GetDataResponse> 
</soap:Body> 
</soap:Envelope> 

任何人能請給我如何消費這樣的服務的示例代碼。

非常感謝提前!

這是我已經用於調用web服務

<?php 

$soap_client = new SoapClient("http://172.160.0.49/OxiWalletService/Service.asmx?WSDL"); 

$Uid='oxigen'; 
$Pwd='oxigen'; 
$ns = "http://tempuri.org/"; 

//Body of the Soap Header. 
$headerbody = array('UserName' => $Uid, 
        'Password' => $Pwd 
        ); 
//Create Soap Header.  
$header = new SOAPHeader($ns, 'AuthHeader', $headerbody);  

//set the Headers of Soap Client. 
$soap_client->__setSoapHeaders($header); 
$par="<Wallet><SPName>AuthenticateMerchantWebVending</SPName><Parameters>&lt;Parameter&gt;&lt;Name&gt;@Account&lt;/Name&gt;&lt;Size&gt;50&lt;/Size&gt;&lt;Value&gt;1135600016&lt;/Value&gt;&lt;Type&gt;varchar&lt;/Type&gt;&lt;/Parameter&gt;&lt;Parameter&gt;&lt;Name&gt;@Password&lt;/Name&gt;&lt;Size&gt;20&lt;/Size&gt;&lt;Value&gt;0OgknrdonyM=&lt;/Value&gt;&lt;Type&gt;varchar&lt;/Type&gt;&lt;/Parameter&gt;</Parameters><ParameterCount>2</ParameterCount><DataBase>1</DataBase></Wallet>"; 
$param=array('xmlString'=>$par); 

$result=$soap_client->__SoapCall('WS_GetData',$param); 

print_r ($result); 

?> 

代碼和我得到以下作爲輸出:

stdClass的對象([WS_GetDataResult] => 2Unknown錯誤)

想法??

因此,原來你已經通過第二個參數與參數數組

意義的關鍵這一

$result=$soap_client->__SoapCall('WS_GetData',$param); 

應該

$result=$soap_client->__SoapCall('WS_GetData',array('parameters'=>$param)); 

這就是現在的工作。

+0

http://stackoverflow.com/questions/1581169/invoking-net-web-service-with-php – Arfeen 2012-01-30 10:21:33

+0

還要檢查HTTP:// www.scottnichol.com/nusoapprogwsdl.htm教程 – Arfeen 2012-01-30 10:23:41

回答

2

我想這應該做的伎倆: www.php.net/manual/en/soapclient.setsoapheaders.php

$ns = "http://tempuri.org/" 

//Body of the Soap Header. 
$headerbody = array('UserName' => $yourUsername, 
        'Password' => $yourPassword, 
      ); 

//Create Soap Header.  
$header = new SOAPHeader($ns, 'AuthHeader', $headerbody);  

//set the Headers of Soap Client. 
$soap_client->__setSoapHeaders($header); 
+0

好的,這是第一個。即使我有啓用的SOAPClient我得到這個錯誤 未定義的變量:soap_client在C:\ WAMP \ WWW \ soap1.php上線14 和 致命錯誤:調用一個成員函數__setSoapHeaders()在第14行的C:\ wamp \ www \ soap1.php中沒有對象 – Tanmay 2012-01-30 11:01:45

+0

好吧,你實際上需要有soap客戶端實例,我的代碼是部分的。在沒有真實數據的情況下發布您的代碼,我們將嘗試查看您的錯誤。 – 2012-01-30 11:04:18

+0

對我來說,WSDL甚至不會從url加載。 – 2012-01-30 11:52:16