PHP XML Soap请求 - [env:Server]无效的凭据

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:
" . $client->__getLastRequest() . "<br /><br />";
echo "REQUEST HEADERS:
" . $client->__getLastRequestHeaders() . "<br /><br />";
echo "RESPONSE:
" . $client->__getLastResponse() . "<br /><br />";
echo "RESPONSE HEADERS:
" . $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?

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.