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

时间:2015-03-25 04:34:30

标签: php xml soap wsdl

我正在尝试通过PHP为WSDK服务创建XML SOAP请求 - 我的代码如下:

<?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);

?>

XML请求应采用以下格式:

<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>

目前我收到错误:

&#34;致命错误:未捕获的SoapFault异常:[env:Server]凭据无效&#34;

  • 如果我使用普通数组而不是SoapVar,我就不会收到凭据错误 - 我的用户名和密码肯定是正确的。

如何使用PHP将请求转换为正确的XML请求格式?

1 个答案:

答案 0 :(得分:0)

最后我的代码还可以,但我仍然遇到问题。

最终解决方案是清除WSDL缓存: In PHP how can you clear a WSDL cache?

我使用两种方法来清除缓存(init和动态)

[UPDATE] 仍然有无效的服务器凭据错误问题,不知道为什么。虽然清除缓存似乎有一段时间了。