doushou9028 2015-03-25 04:34
浏览 342

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?

  • 写回答

1条回答 默认 最新

  • doushan2811 2015-04-16 23:26
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题