将php soap调用转换为节点

时间:2019-02-21 22:27:14

标签: javascript php node.js soap node-soap

请注意:我在URL和方法中使用占位符

尝试创建一个将使用node-soap消耗soap feed的应用程序。

我可以使用PHP(我更熟悉的语言)生成预期的输出:

$client = new SOAPClient('https://soap.wsdl', array('trace' => 1, 'cache_wsdl' => WSDL_CACHE_NONE));
$soap_var = "<ns1:DS_REQ class='R'><ns1:YEAR>2018</ns1:YEAR></ns1:DS_REQ>";
$request = array('DS_REQ' => array('DS_REQ' => new SoapVar($soap_var, XSD_ANYXML)));
$getMethod = $client->__soapCall('get_method', array('parameters' => $request));
var_dump($getMethod);

但是,当我使用节点肥皂时:

"use strict";

const soap = require('soap');
let url = 'https://soap.wsdl';
let args = {
    _xml: "<ns1:DS_REQ class='R'><ns1:YEAR>2018</ns1:YEAR></ns1:DS_REQ>"
};

soap.createClient(url, function(err, client){
    console.log(client.describe());
    client.GET_METHOD(args, function(err, result, rawResponse, soapHeader, rawRequest) {
        console.log(rawResponse);
    })
});

我正在正确连接client.describe返回预期的输出。

client.describe()
{ UDS_ACAD:
   { DS_ACAD_Port:
      { GET_ATTR_DEFS: [Object],
        GET_METHOD: [Object],
        GET_METHOD2: [Object] } } }

但是rawResponse返回错误 “ ...空响应HTTPBridge接收到一个空表示,无法将其转换为合适的HTTP响应...” result和err均为空。

我很确定我正确地传递了参数,但是我对node还是很陌生,并试图在解决过程中弄清楚。



更新:能够使用强肥皂性和同事的大量帮助

这是工作代码...

"use strict";

let soap = require('strong-soap').soap;
let WSDL = soap.WSDL;
let url = 'https://soap.wsdl';

function constructArguments(year) {
  let variables = {
    UDS_ACAD: {
      DS_REQ: {
        $attributes: {
          class: 'R'
        },
      }
    }
  };

  if (year != null ) {
    variables.UDS_ACAD.DS_REQ.YEAR = year;
  }
  return variables;
}

// Pass in WSDL options if any
let options = {};
WSDL.open(url,options, function(err, wsdl) {
  // You should be able to get to any information of this WSDL from this object. Traverse
  // the WSDL tree to get  bindings, operations, services, portTypes, messages,
  // parts, and XSD elements/Attributes.

  // Set the wsdl object in the cache. The key (e.g. 'stockquotewsdl')
  // can be anything, but needs to match the parameter passed into soap.createClient()
  let clientOptions = {
    WSDL_CACHE : {
      mywsdl: wsdl
    }
  };

  soap.createClient('mywsdl', clientOptions, function(err, client) {
    let year = '2018';
    let requestArguments = constructArguments(year);

    const myRequestMethod = async () => {
      try {
        const {result, envelope, soapHeader} = await client.GET_METHOD(requestArguments);
      return result;
      } catch(err) {
        // handle error
       console.log(err);
      }
    };

    const processData = async () => {
      const myRequest = await myRequestMethod();
      console.log(myRequest);
    }; 

    processData();

  });
});

0 个答案:

没有答案