使用 PHP 使用 .Net Web 服务 [英] Consume a .Net web service using PHP
问题描述
这是我第一次使用网络服务/SOAP...我一直在尝试使用 PHP 使用 .Net 网络服务,但无济于事.我已经搜索并阅读了谷歌抛出的所有与此相关的页面,但我仍然迷路了.
This is my first time with web services/SOAP...i have been trying to consume .Net web services using PHP but to no avail. I have searched and read all pages that google throws up for anything related to this but i am still lost.
问题是我试图调用的 SOAP 服务有一个授权标头,我想不出一种方法来验证我的请求.
The thing is the SOAP service i am trying to call has an authorization header and i can't figure out a way to authenticate my request.
我已经尝试了 php-soapclient 和 NuSoap,但没有可用的示例代码可以提供帮助.所以任何帮助都会很棒.
I have tried the php-soapclient and NuSoap both but there is no sample code available that would help. So any help would be great.
以下是一个示例 SOAP 1.1 请求和响应.
The following is a sample SOAP 1.1 request and response.
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>
任何人都可以提供有关如何使用此类服务的示例代码.
Can anybody please gimme a sample code on how to consume such a service.
非常感谢!
这是我用来调用网络服务的代码
This is the code that i have used to call the web service
<?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><Parameter><Name>@Account</Name><Size>50</Size><Value>1135600016</Value><Type>varchar</Type></Parameter><Parameter><Name>@Password</Name><Size>20</Size><Value>0OgknrdonyM=</Value><Type>varchar</Type></Parameter></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 Error )
stdClass Object ( [WS_GetDataResult] => 2Unknown Error )
想法??
所以事实证明你必须传递带有参数的第二个参数作为数组的键
So it turns out you've to pass the second argument with parameters as the key of the array
这个意思
$result=$soap_client->__SoapCall('WS_GetData',$param);
应该
$result=$soap_client->__SoapCall('WS_GetData',array('parameters'=>$param));
现在可以使用了.
推荐答案
我认为这应该可以解决问题:www.php.net/manual/en/soapclient.setsoapheaders.php
I think this should do the trick: 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);
这篇关于使用 PHP 使用 .Net Web 服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!