2012-11-20 2 views
13

PHP SoapClient 헤더. 자식 노드에서 네임 스페이스를 가져 오는 데 문제가 있습니다. 내가 네임 스페이스를 얻을 필요가SoapHeader 자식 노드의 PHP 네임 스페이스

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

을 : 여기

$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이있어 여기에 내가 사용하고 코드입니다 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