dongshou2017 2012-01-30 10:17
浏览 51
已采纳

使用PHP消费.Net Web服务

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.

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.

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.

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>

Response

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.

Many thanks in advance!

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>&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);

?>

and i am getting the following as output:

stdClass Object ( [WS_GetDataResult] => 2Unknown Error )

Ideas??

So it turns out you've to pass the second argument with parameters as the key of the array

meaning this

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

should be

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

This works now.

  • 写回答

1条回答 默认 最新

  • dsgwoh7038 2012-01-30 10:25
    关注

    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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据