簡體   English   中英

使用PHP消費.Net Web服務

[英]Consume a .Net web service using PHP

這是我第一次使用Web服務/ SOAP ...我一直在嘗試使用PHP來使用.Net Web服務,但無濟於事。 我已經搜索並閱讀了所有與谷歌相關的網頁,但我仍然輸了。

問題是我試圖調用的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));

這現在有效。

我認為這應該可以解決這個問題: 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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM