2012-11-20 51 views
13

PHP SoapClient頭。我在獲取子節點中的名稱空間時遇到問題。下面是我使用的代碼:SoapHeader子節點中的PHP命名空間

$security = new stdClass; 
$security->UsernameToken->Password = 'MyPassword'; 
$security->UsernameToken->Username = 'MyUsername'; 
$header[] = new SOAPHeader('http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd', 'Security', $security); 
$client->__setSoapHeaders($header); 

下面是它生成XML:

<ns2:Security> 
    <UsernameToken> 
    <Password>MyPassword</Password> 
    <Username>MyUsername</Username> 
    </UsernameToken> 
</ns2:Security> 

這是我想它來生成XML:

<ns2:Security> 
    <ns2:UsernameToken> 
    <ns2:Password>MyPassword</ns2:Password> 
    <ns2:Username>MyUsername</ns2:Username> 
    </ns2:UsernameToken> 
</ns2:Security> 

我需要得到的命名空間參考UsernameToken,Password和Username節點。任何幫助將非常感激。

謝謝。

回答

9

想通了。我使用嵌套的SoapVars和數組。

$ns_s = 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd'; 
$node1 = new SoapVar('MyUsername', XSD_STRING, null, null, 'Username', $ns_s); 
$node2 = new SoapVar('MyPassword', XSD_STRING, null, null, 'Password', $ns_s); 
$token = new SoapVar(array($node1,$node2), SOAP_ENC_OBJECT, null, null, 'UsernameToken', $ns_s); 
$security = new SoapVar(array($token), SOAP_ENC_OBJECT, null, null, 'Security', $ns_s); 
$header[] = new SOAPHeader($ns_s, 'Security', $security, false); 

那花了太多精力和想法...

11

Davidright answer。而且他也是這樣對待的方式太費勁了,想到。這裏有一個變體,它爲工作這個特定的wsse安全頭文件的任何人封裝了醜陋之處。

清潔客戶端代碼

$client = new SoapClient('http://some-domain.com/service.wsdl'); 
$client->__setSoapHeaders(new WSSESecurityHeader('myUsername', 'myPassword')); 

和實現...

class WSSESecurityHeader extends SoapHeader { 

    public function __construct($username, $password) 
    { 
     $wsseNamespace = 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd'; 
     $security = new SoapVar(
      array(new SoapVar(
       array(
        new SoapVar($username, XSD_STRING, null, null, 'Username', $wsseNamespace), 
        new SoapVar($password, XSD_STRING, null, null, 'Password', $wsseNamespace) 
       ), 
       SOAP_ENC_OBJECT, 
       null, 
       null, 
       'UsernameToken', 
       $wsseNamespace 
      )), 
      SOAP_ENC_OBJECT 
     ); 
     parent::SoapHeader($wsseNamespace, 'Security', $security, false); 
    } 

} 
+0

感謝我用這個,但改了密碼部分: 新SoapVar($密碼,XSD_STRING, 'type','http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText','Password',$ wsseNamespace)), 這給你: yourpassword Craig

相關問題