drl9940 2012-12-04 15:42
浏览 39
已采纳

使用SimpleXML创建WS-Security标头?

I want to create a WSS header to authentificate on secured web services.

I can do it using an ugly :

    $auth = '
     <wsse:Security SOAP-ENV:mustUnderstand="1" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
        <wsu:Timestamp wsu:Id="Timestamp-28" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
            <wsu:Created>' . $timestamp . '</wsu:Created>
            <wsu:Expires>' . $timestampExpires . '</wsu:Expires>
        </wsu:Timestamp>
        <wsse:UsernameToken wsu:Id="UsernameToken-27" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
            <wsse:Username>' . $user . '</wsse:Username>
            <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordDigest">' . $passdigest . '</wsse:Password>
            <wsse:Nonce EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary">' . $encodedNonce . '</wsse:Nonce>
            <wsu:Created>' . $timestamp . '</wsu:Created>
        </wsse:UsernameToken>
     </wsse:Security>';

I am now trying to do it cleaner, using SimpleXML.

But if I try to do a simple :

    $xml = new SimpleXMLElement('<wsse:Security/>', 0, false, 'wsse');

I get :

warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: namespace error : Namespace prefix wsse on Security is not defined in

I think I miss something with the way to create namespaced xmls, can you give me some hints?

  • 写回答

1条回答 默认 最新

  • douwei2713 2012-12-04 16:31
    关注

    I found a way to solve my problem :

    $root = new SimpleXMLElement('<root/>');
    
    $security = $root->addChild('wsse:Security', 'test', 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd');
    
    $root->registerXPathNamespace('wsse', 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd');
    $auth = $root->xpath('/root/wsse:Security');
    echo htmlentities($auth[0]->asXML());
    

    Displays :

    <wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">test</wsse:Security> 
    

    And also, there is a mistake in my XML, I put a SOAP-ENV:mustUnderstand="1" but I never define the SOAP-ENV namespace.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大