PHP SOAP Security header


I'm starting with SOAP / PHP and have to send a security. IT's drving me crazy, I have tried a dozen approachesd found here but nothing works. Here is the header excpected by the service - how can I send it?

        <wsse:Security soapenv:mustUnderstand="1"
            <wsse:UsernameToken wsu:Id="UsernameToken-6">
                <wsse:Password  Type="">
                <wsse:Nonce EncodingType="">

I used this approach last: Connecting to WS-Security protected Web Service with PHP and get following error:

public 'faultstring' => string 'An error was discovered processing the header' (length=61) public 'faultcode' => string 'ns1:InvalidSecurity' (length=19)

I really appreciate every help - thanks in advance.

Here is my current php code.

 * This function implements a WS-Security digest authentification for PHP.
 * @access private
 * @param string $user
 * @param string $password
 * @return string
function generateWSSecurity($user, $password)
    // Creating date using yyyy-mm-ddThh:mm:ssZ format
    $tm_created = gmdate('Y-m-d\TH:i:s\Z');
    $tm_expires = gmdate('Y-m-d\TH:i:s\Z', gmdate('U') + 180);

    // Generating, packing and encoding a random number
    $simple_nonce = mt_rand();
    $encoded_nonce = base64_encode(pack('H*', $simple_nonce));

    // Compiling WSS string
    $passdigest = base64_encode(pack('H*',sha1(pack('H*', $simple_nonce) . pack('a*', $tm_created) . pack('a*', $password))));

    // Initializing namespaces
    $ns_envelope = '';
    $ns_wsse = '';
    $ns_wsu = '';
    $password_type = '';
    $encoding_type = '';

    // Creating WSS identification header using SimpleXML
    $root = new \SimpleXMLElement('<root/>');

    $envelope = $root->addChild('soapenv:Envelope', null, $ns_envelope);

    $soapheader = $envelope->addChild('soapenv:Header');

    $security = $soapheader->addChild('wsse:Security', null, $ns_wsse);

    $usernameToken = $security->addChild('wsse:UsernameToken', null, $ns_wsse);
    $usernameToken->addChild('wsse:Username', $user, $ns_wsse);

    $password = $usernameToken->addChild('wsse:Password', $passdigest, $ns_wsse);
    $password->addAttribute('Type', $password_type);

    $nonce = $usernameToken->addChild('wsse:Nonce', $encoded_nonce, $ns_wsse);
    $nonce->addAttribute('EncodingType', $encoding_type);

    $usernameToken->addChild('wsu:Created', $tm_created, $ns_wsu);

    // Recovering XML value from that object
    $root->registerXPathNamespace('soapenv', $ns_envelope);
    $full = $root->xpath('/root/soapenv:Envelope');
    $auth = $full[0]->asXML();
    return $auth;

function soapClientWSSecurityHeader($user, $password)
    return new \SoapHeader('',
            'Security', new \SoapVar(generateWSSecurity($user, $password), XSD_ANYXML), true

$client = new \SoapClient('https://webservice?wsdl');
$client->__setSoapHeaders(soapClientWSSecurityHeader('user', 'pass'));

It works so far, but now I get this if I call any operation:

public 'faultstring' => string 'Bad credentials' (length=15) public 'faultcode' => string 'soap:Server' (length=11)

Any ideas?

People are also looking for solutions to the problem: php - Extract a Flickr photo ID from URL


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.