2016-04-25 115 views
0

我想在PHP中使用SoapClient进行SOAP调用,但我无法发送身体参数。标题正在发送完好,没有错误,但正文是空的。PHP SoapClient身体参数不被发送

我对PHP不是很熟悉,所以这对我来说是新的。

这里是我的登录功能:

function Login($username, $password, $clientaccesskey, $useraccesskey) 
{ 
    $parameters = array(
        'LogOnRequest' => 
          array(
           'ClientAccessKey' => $clientaccesskey, 
           'Password' => $password, 
           'UserAccessKey' => $useraccesskey, 
           'UserName' => $username 
           ) 
        ); 

    $headers = array(); 

    $headers[] = new SoapHeader('http://www.w3.org/2005/08/addressing', 'Action', 'http://www.ultipro.com/dataservices/bidata/2/IBIDataService/LogOn'); 
    $headers[] = new SoapHeader('http://www.w3.org/2005/08/addressing', 'To', 'https://service4.ultipro.com/services/BiDataService'); 

    try 
    { 
     $soapclient = new SoapClient('https://service4.ultipro.com/services/BiDataService', array('soap_version' => SOAP_1_2, 'encoding' => 'UTF-8', 'exceptions' => TRUE, 'trace' => TRUE)); 
     $soapclient->__setSoapHeaders($headers); 
     $response = $soapclient->__soapCall('LogOn', array($parameters)); 

     echo json_encode($response); 

     echo htmlentities($soapclient->__getLastRequest()); 
    } 
    catch(SoapFault $fault){ 
     echo $fault->faultstring; 
    } 

} 

当我运行这段代码似乎是在讨论到服务器,因为我得到的回应:

{"LogOnResult":{"ServiceId":null,"ClientAccessKey":null,"Token":null,"Status":"Failed","StatusMessage":"User authentication failed","InstanceKey":null}} 

但是,这是XML是正在发送到该服务:

<?xml version="1.0" encoding="UTF-8"?> 
<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope" xmlns:ns1="http://www.ultipro.com/dataservices/bidata/2" xmlns:ns2="http://www.w3.org/2005/08/addressing"> 
    <env:Header> 
     <ns2:Action>http://www.ultipro.com/dataservices/bidata/2/IBIDataService/LogOn</ns2:Action> 
     <ns2:To>https://service4.ultipro.com/services/BiDataService</ns2:To> 
    </env:Header> 
    <env:Body> 
     <ns1:LogOn /> 
    </env:Body> 
</env:Envelope> 

这就是XML所支持的样子:

<?xml version="1.0" encoding="UTF-8"?> 
<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns:a="http://www.w3.org/2005/08/addressing"> 
    <s:Header> 
     <a:Action s:mustUnderstand="1">http://www.ultipro.com/dataservices/bidata/2/IBIDataService/LogOn</a:Action> 
     <a:To s:mustUnderstand="1">https://servicehost/services/BiDataService</a:To> 
    </s:Header> 
    <s:Body> 
     <LogOn xmlns="http://www.ultipro.com/dataservices/bidata/2"> 
     <logOnRequest xmlns:i="http://www.w3.org/2001/XMLSchema-instance"> 
      <UserName>username</UserName> 
      <Password>password</Password> 
      <ClientAccessKey>12345</ClientAccessKey> 
      <UserAccessKey></UserAccessKey> 
     </logOnRequest> 
     </LogOn> 
    </s:Body> 
</s:Envelope> 

我在做什么错误导致身体参数不在SOAP请求中发送?

回答

2

我可以用一个类来实现它,则SOAPVarSOAPParam类,像这样:

function Login($username, $password, $clientaccesskey, $useraccesskey) 
{ 
    // Class handler for the request 
    class LogOnRequest { 
     function __construct($usr, $pwd, $cak, $uak) 
     { 
      $this->UserName = $usr; 
      $this->Password = $pwd; 
      $this->ClientAccessKey = $cak; 
      $this->UserAccessKey = $uak; 
     } 
    } 

    // Conversion to a SOAP object 
    $lor = new LogOnRequest($username, $password, $clientaccesskey, $useraccesskey); 
    $logOnRequest = new SoapVar($lor, SOAP_ENC_OBJECT, 'LogOnRequest', 'http://soapinterop.org/xsd'); 

    $headers = array(); 
    $headers[] = new SoapHeader('http://www.w3.org/2005/08/addressing', 'Action', 'http://www.ultipro.com/dataservices/bidata/2/IBIDataService/LogOn'); 
    $headers[] = new SoapHeader('http://www.w3.org/2005/08/addressing', 'To', 'https://service4.ultipro.com/services/BiDataService'); 

    try 
    { 
     $soapclient = new SoapClient('https://service4.ultipro.com/services/BiDataService', array('soap_version' => SOAP_1_2, 'encoding' => 'UTF-8', 'exceptions' => TRUE, 'trace' => TRUE)); 
     $soapclient->__setSoapHeaders($headers); 

     // Direct call to the LogOn method and SOAP parameter pass 
     $response = $soapclient->LogOn(new SoapParam($logOnRequest, 'logOnRequest')); 

     echo json_encode($response); 

     echo htmlentities($soapclient->__getLastRequest()); 
    } 
    catch(SoapFault $fault){ 
     echo $fault->faultstring; 
    } 

} 

这给了我下面的XML请求:

<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope" xmlns:ns1="http://soapinterop.org/xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns2="http://www.ultipro.com/dataservices/bidata/2" xmlns:ns3="http://www.w3.org/2005/08/addressing"> 
    <env:Header> 
     <ns3:Action>http://www.ultipro.com/dataservices/bidata/2/IBIDataService/LogOn</ns3:Action> 
     <ns3:To>https://service4.ultipro.com/services/BiDataService</ns3:To> 
    </env:Header> 
    <env:Body> 
     <ns2:LogOn xsi:type="ns1:LogOnRequest"> 
      <UserName>u</UserName> 
      <Password>p</Password> 
      <ClientAccessKey>cak</ClientAccessKey> 
      <UserAccessKey>uak</UserAccessKey> 
     </ns2:LogOn> 
    </env:Body> 
</env:Envelope> 
+0

感谢您的帮助。我仍然无法使用web服务登录。我唯一不知道的是这个“'这是否需要进一步嵌套或者你认为你要输出什么? – user3788671

+0

我认为它应该工作,但你最好问肥皂服务的所有者,看看应该如何发送数据。 –

+0

好的,谢谢! – user3788671