1

I am trying to create an XML SOAP request to a WSDK service via PHP - my code is as follows:

<?php

        //WSSE Authentication Header Object
class WsseAuthHeader extends SoapHeader {

    private $wss_ns = 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd';

    function __construct($user, $pass, $ns = null) {
        if ($ns) {
            $this->wss_ns = $ns;
        }

        $auth = new stdClass();
        $auth->Username = new SoapVar($user, XSD_STRING, NULL, $this->wss_ns, NULL, $this->wss_ns);
        $auth->Password = new SoapVar($pass, XSD_STRING, NULL, $this->wss_ns, NULL, $this->wss_ns);

        $username_token = new stdClass();
        $username_token->UsernameToken = new SoapVar($auth, SOAP_ENC_OBJECT, NULL, $this->wss_ns, 'UsernameToken', $this->wss_ns);

        $security_sv = new SoapVar(
                new SoapVar($username_token, SOAP_ENC_OBJECT, NULL, $this->wss_ns, 'UsernameToken', $this->wss_ns), SOAP_ENC_OBJECT, NULL, $this->wss_ns, 'Security', $this->wss_ns);
        parent::__construct($this->wss_ns, 'Security', $security_sv, true);
    }

}

//Set WSSE Variables
$username = 'username';
$password = 'pw';
$wsdl = 'wsdlurl';

$wsse_header = new WsseAuthHeader($username, $password);

//Create Soap Client
$client = new SoapClient($wsdl,array("trace" => 1));

//Set SOAP headers
$client->__setSoapHeaders(array($wsse_header));


$xml_text =  "<v1:MembershipNumber>999999</v1:MembershipNumber>";

$v1 = new SoapVar($xml_text, 147);



//Create Object
$request = array(
        "MembershipNumber"  => $v1,
);

var_dump($request);

//$results = $client->GetMemberNumber($request);
$results = $client->__soapCall('GetMemberNumber', array('MemberNumberRequest'=>$request));
//Print Results
echo "<br /><br />REQUEST:\n" . $client->__getLastRequest() . "<br /><br />";
echo "REQUEST HEADERS:\n" . $client->__getLastRequestHeaders() . "<br /><br />";
echo "RESPONSE:\n" . $client->__getLastResponse() . "<br /><br />";
echo "RESPONSE HEADERS:\n" . $client->__getLastResponseHeaders() . "<br /><br />";
echo "Var Dump: "; var_dump($results);

?>

The XML Request should be in the following format:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:v1="schemaurl">
   <soapenv:Header>
      <wsse:Security soapenv:mustUnderstand="1" 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-1">
            <wsse:Username>username</wsse:Username>
            <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">password</wsse:Password>
         </wsse:UsernameToken>
      </wsse:Security>
   </soapenv:Header>
   <soapenv:Body>
      <v1:MemberNumberRequest>
         <v1:MemberNumber>?</v1:MemberNumber>
      </v1:MemberNumberRequest>
   </soapenv:Body>
</soapenv:Envelope>

At the moment I am getting the error:

"Fatal error: Uncaught SoapFault exception: [env:Server ] Invalid Credentials"

  • If I use a regular array instead of the SoapVar's I do not get the credentials error - my username and password are definitely correct.

How do I get the request into the correct XML request format using PHP?

L0ckz0r
  • 169
  • 1
  • 9
  • The error message signals invalid credentials, so most likely the WsseAuthHeader doesn't ship the correct credentials *or* has a problem encoding them (or both). Where did you copy the **WsseAuthHeader** code from? Has it been reported working there? Is this constantly tested automatically? – hakre Mar 26 '15 at 18:44
  • Thanks for your help. In the end the problem ended up being at the service end, I was not provided the correct set of test data. | So the credentials and the call was correct. – L0ckz0r Mar 29 '15 at 23:21
  • Although for anyone who stumbles upon this - the developers changed something on the service- Originally this worked: `//Set SOAP headers $client->__setSoapHeaders(array($wsse_header)); $results = $client->__soapCall('FunctionName', array('FunctionRequest'=>$request));` Then I had to change it to: `$results = $client->__soapCall('FunctionName', array('FunctionRequest'=>$request), null, array($wsse_header))` Finally I had to go back to the first option to get it to work, I'm not sure what changed. – L0ckz0r Mar 29 '15 at 23:24
  • You can leave your solution as an answer below and mark it as the answer. This will make visible what your solution was and that it worked for you. – hakre Mar 30 '15 at 07:04

1 Answers1

0

In the end my code was okay but I was still having problems.

Final solution was to clear WSDL cache: In PHP how can you clear a WSDL cache?

I used both methods to clear the cache (init and dynamically)

[UPDATE] Still have problems with the invalid server credentials error every now and then, not sure why. Though clearing cache seemed to work for a little while.

Community
  • 1
  • 1
L0ckz0r
  • 169
  • 1
  • 9