如何在PHP上设置SOAP标头以验证Web服务?

时间:2013-09-19 09:25:20

标签: php web-services soap wsdl ws-security

我需要将SOAP标头设置为以下格式:

<soapenv:Header>
    <wsse:Security 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-45">
            <wsse:Username>XXXXX</wsse:Username>
            <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">XXXXXXX</wsse:Password>
            <wsse:Nonce EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary">OxWtCYYj1cX7HiZeMEqorw==</wsse:Nonce>
            <wsu:Created>2013-09-18T07:25:50.227Z</wsu:Created>
        </wsse:UsernameToken>
    </wsse:Security>
</soapenv:Header>

我尝试使用下面的代码验证网络服务,但它没有用。

$momurl = "https://integrationdev.momentum.co.za/sales/CRMService/CRMLeadService_v1_0/";

$client = new SoapClient(
    "$momurl",
    array(
        'trace' => 1,
        'login' => $username,
        'password' => $password
    )
);

任何帮助将不胜感激。提前谢谢。

1 个答案:

答案 0 :(得分:5)

您需要使用SoapClient::__setSoapHeader()。像这样:

$security = array(
    'Username'=>'XXXXX',
    'Password'=>'XXXXX',
    'Nonc'=> 'OxWtCYYj1cX7HiZeMEqorw==',
    'Created' => '2013-09-18T07:25:50.227Z',
    'UsernameToken' => NULL
);

$header = new SoapHeader('wsse','Security',$security, false);
$client->__setSoapHeaders($header);