【问题标题】:SOAP-Request works with PHP but throws Syntax-Error with NodeJSSOAP-Request 与 PHP 一起工作,但在 NodeJS 中抛出语法错误
【发布时间】:2018-08-08 15:07:26
【问题描述】:

我正在使用给定的 SOAP-API 来请求一些东西。该 API 有据可查,但所有示例都是用 PHP 编写的,而不是我喜欢的语言。我在 PHP 中的所有实现都有效,但现在我想在我的 ExpressJS-Service 中实现和集成它。我正在使用流行的 NodeJS-SOAP-Library 和完全相同的请求数据。但是对 API 的请求会返回以下错误信息:

    <?xml version="1.0" encoding="UTF-8"?>\n
        <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
        <SOAP-ENV:Body>
            <SOAP-ENV:Fault>
                <faultcode>SOAP-ENV:Client</faultcode><faultstring>session_lifetime_syntax_incorrect:w</faultstring><faultactor>KasAuth</faultactor>
            </SOAP-ENV:Fault>
        </SOAP-ENV:Body>
    </SOAP-ENV:Envelope>\n

生成的请求正文如下所示:

<?xml version="1.0" encoding="utf-8"?>
   <soap:Envelopexmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xmlns:tns="https://kasserver.com/">
        <soap:Body>
            <tns:KasAuth><KasUser>[username]</KasUser>
            <KasAuthType>sha1</KasAuthType>
            <KasPassword>[password-hash]</KasPassword>
            <SessionLifeTime>600</SessionLifeTime>
            <SessionUpdateLifeTime>Y</SessionUpdateLifeTime>
            </tns:KasAuth>
        </soap:Body>
    </soap:Envelope>

我的 JavaScript 代码如下所示:

var soap = require('soap');
var sha1 = require('js-sha1');

var password = sha1('[password]');

var url = '[api-url].wsdl';
var args = {
    KasUser: '[username]',
    KasAuthType: 'sha1',
    KasPassword: password,
    SessionLifeTime: 600,
    SessionUpdateLifeTime: 'Y'
};

soap.createClient(url, function(err, client) {
    client.KasAuth(args, function(err, result) {
        console.log(result);
    });
});

我的工作 PHP 代码如下所示:

...
$session_lifetime = 600;
$session_update_lifetime = 'Y';
...
try
    {
      $SoapLogon = new SoapClient('[api-url].wsdl');
      $CredentialToken = $SoapLogon->KasAuth(json_encode(array(
                            'KasUser' => $kas_user,
                            'KasAuthType' => 'sha1',
                            'KasPassword' => sha1($kas_pass),
                            'SessionLifeTime' => $session_lifetime,
                            'SessionUpdateLifeTime' => $session_update_lifetime
                            )));

      header('Content-Type: application/json');
      echo(json_encode(array('token' => $CredentialToken), JSON_PRETTY_PRINT));

    }

    // Fehler abfangen und ausgeben
    catch (SoapFault $fault)
    {
        trigger_error("Fehlernummer: {$fault->faultcode},
                        Fehlermeldung: {$fault->faultstring},
                        Verursacher: {$fault->faultactor},
                        Details: {$fault->detail}", E_USER_ERROR);
    }

我的工作 PHP 代码的请求正文:

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="urn:xmethodsKasApiAuthentication" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
  <SOAP-ENV:Body>
    <ns1:KasAuth>
      <Params xsi:type="xsd:string">{"KasUser":[username],"KasAuthType":"sha1","KasPassword":[password-hash],"SessionLifeTime":600,"SessionUpdateLifeTime":"Y"}</Params>
    </ns1:KasAuth>
  </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

登录凭据相同。

我尝试将 SessionLifeTime 的值更改为 600.0 和 '600'...

API-Provider 不知道可能出了什么问题。

更新: 我看到请求主体不同,但我不知道如何在 node-soap 中更改它。

【问题讨论】:

    标签: javascript php node.js express soap


    【解决方案1】:

    最大的不同是php版本把所有的params都变成了一个json字符串塞进Params元素,而js版本把js对象编码成xml。我会让 js 端匹配 JSON.stringify(args)} 之类的东西。

    【讨论】:

      猜你喜欢
      • 2021-12-26
      • 1970-01-01
      • 2012-01-18
      • 1970-01-01
      • 1970-01-01
      • 2019-05-21
      • 2015-03-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多