SOAP PHP请求错误

时间:2012-03-27 16:32:51

标签: php api soap soap-client

我正在使用第三方API来请求余额。我在不同的尝试中遇到了很多错误,但我知道我很接近。这是我的电话代码...

<?php 
try {   
$wsdl_url = 'https://www.domain.com/SOAP?wsdl';
$UserID = 'UserID';
$Pwd= 'Password';
$client = new SOAPClient($wsdl_url);
$headerbody = array( 
                     'UsernameToken'=>array('Username'=>$UserID, 
                                             'Password'=>$Pwd)); 

//Create Soap Header.        
$header = new SOAPHeader('wsse', 'Security', $headerbody);        

//set the Headers of Soap Client. 
$client->__setSoapHeaders($header); 
//$client->__getLastResponse();
$BalanceInquiryRequest->AccountNo="7777700020";
$BalanceInquiryRequest->RetailerId="0123";

$getBalance = $client->balanceInquiry($BalanceInquiryRequest);
print_r($getBalance); 
} catch (SoapFault $fault) {
echo $fault->faultcode . "-" . $fault->faultstring;  
//echo "REQUEST:\n" . htmlentities($client->__getLastRequest()) . "\n";
}

?>

这是......

的输出

ENV:服务器显示java.lang.NullPointerException

这里是请求的xml ...

<soapenv:Envelope xmlns:req="http://domain.com" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
   <soapenv:Header>
      <wsse:Security soapenv:mustUnderstand="0" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
         <wsse:UsernameToken wsu:Id="UsernameToken-1">
            <wsse:Username>Username</wsse:Username>
            <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">Password</wsse:Password>
            <wsse:Nonce EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary">5Ht+O9hKvAEXhNJZSTLg==</wsse:Nonce>
            <wsu:Created>2012-03-27T15:31:01.792Z</wsu:Created>
         </wsse:UsernameToken>
      </wsse:Security>
   </soapenv:Header>
   <soapenv:Body>
      <req:BalanceInquiryRequest>
         <req:AccountNo>7777700020</req:AccountNo>
         <req:RetailerId>0123</req:RetailerId>
      </req:BalanceInquiryRequest>
   </soapenv:Body>
</soapenv:Envelope>

1 个答案:

答案 0 :(得分:0)

好的,问题是我的代码中的WSSE是一个与PHP SOAPClient不兼容的标准。我做了一些研究,发现有一些方法可以让这两个很好地结合在一起。我所做的是先确定名称空间,用户名和密码......

$username = 'USERNAME';
$password = 'PASSWORD';

$strWSSENS = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd";

接下来是创建两个处理此信息的类(在线帮助)......

class clsWSSEAuth {
  private $Username;
  private $Password; 
    function __construct($username, $password) {
      $this->Username=$username;
      $this->Password=$password;
    }
}

class clsWSSEToken {
  private $UsernameToken;
    function __construct ($innerVal){
      $this->UsernameToken = $innerVal;
    }
}

然后调用这几个函数来设置标题等......

  $objWSSEAuth = new clsWSSEAuth($objSoapVarUser, $objSoapVarPass);
  $objSoapVarWSSEAuth = new SoapVar($objWSSEAuth, SOAP_ENC_OBJECT, NULL, $strWSSENS, 'UsernameToken', $strWSSENS);
  $objWSSEToken = new clsWSSEToken($objSoapVarWSSEAuth);
  $objSoapVarWSSEToken = new SoapVar($objWSSEToken, SOAP_ENC_OBJECT, NULL, $strWSSENS, 'UsernameToken', $strWSSENS);
  $objSoapVarHeaderVal=new SoapVar($objSoapVarWSSEToken, SOAP_ENC_OBJECT, NULL, $strWSSENS, 'Security', $strWSSENS);
  $objSoapVarWSSEHeader = new SoapHeader($strWSSENS, 'Security', $objSoapVarHeaderVal,true, 'http://abce.com');

然后剩下的就像往常一样......使用SOAPClient ......