.NET Web服务和php客户端

时间:2015-06-27 13:33:57

标签: php web-services soap

我是php客户端与.NET Web服务交互的新手。我环顾四周,知道下面的帖子,但我的问题没有答案。

How to make a PHP SOAP client and store the result xml in php variables

http://www.php.net/manual/en/soapclient.setsoapheaders.php

Consume a .Net web service using PHP

.NET Web服务需要PHP SOAP客户端(用于请求和获取响应) (http://localhost:8080/someexample/myservice.wsdl)。输入/请求如下所示:

<input>
    <the_input name="secondyear">
      <authen>
        <Teacher>
         <teacherid>MrX</teacherid> 
         <password>t_pass</password> 
        </Teacher>
        <Student>
         <studentid>kids</studentid> 
         <password>s_pass</password> 
        </Student>
       </authen>
     <parameters>
        <parameter name="maths" value="123" /> 
        <parameter name="physics" value="abc" />
        <parameter name="sports" value="def" /> 
     </parameters>
    </the_input>
</input>

这是我得到的但没有工作,我不确定我是在正确的轨道上:

<?php 
 require_once("lib/nusoap.php");
 $url="http://example.com/someexample/myservice.wsdl";
 $client = new SoapClient($url);

 $teacherid='MrX';
 $tpassword='t_pass';
 $studentid='kids';
 $spassword='s_pass';
 $ns = "http://example.com/";

 //Body of the Soap Header.
 $headerbody = array ('Teacher' => array('teacherid' => $teacherid,
                                      'password' => $tpassword ),
                      'Student' => array('stendentid' => $studentid,
                                       'password' => $spassword ));
 //Create Soap Header.
 $header = new SOAPHeader($ns, 'Authen', $headerbody);

 //set the Headers of Soap Client.
 $headerparam=$client->__setSoapHeaders($header);

 $name ='secondyear';
 $service_name = array('the_input'=>$name);

 $result =$client->__SoapCall($service_name,$headerparam)
 print_r ($result);
?>

我在某处读取以便对Web服务进行身份验证,应该将身份验证参数放在php标头中,那么什么是php主体呢?还是需要它?是参数吗?该参数具有名称和值的格式,我如何在php数组中执行此操作?是否有相关的例子?如上所述,我完成了我的研究,并不清楚如何解决这个问题。

1 个答案:

答案 0 :(得分:0)

好吧,我想出了自己,我在这里发布了答案,以防它有助于某人遇到同样的问题。 我只是创建一个xml varible:

$myxml ='<the_input name="secondyear">
  <authen>
    <Teacher>
     <teacherid>MrX</teacherid> 
     <password>t_pass</password> 
    </Teacher>
    <Student>
     <studentid>kids</studentid> 
     <password>s_pass</password> 
    </Student>
   </authen>
 <parameters>
    <parameter name="maths" value="123" /> 
    <parameter name="physics" value="abc" />
    <parameter name="sports" value="def" /> 
 </parameters>
</the_input>'

然后将其称为参数(在soap调用函数中):

<?php
 require_once("lib/nusoap.php");
 $url="http://example.com/someexample/myservice.wsdl";
 $client = new SoapClient($url);

 $result =$client->input(array(the_input=>$myxml));
 print_r ($result);
?>

Vola,已连接到网络服务。 当问题得到解决时,看起来很容易。