dtj2ww9500 2019-02-21 22:27
浏览 47

将php soap调用转换为节点

Please Note: I am using placeholders for the url and method

Attempting to create an application that will consume a soap feed using node-soap.

I can can generate the expected output using PHP (a language I am more familiar with):

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

However when I use node-soap:

"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);
    })
});

I am connecting correctly as client.describe returns an expected output.

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

BUT rawResponse returns an error "...Null Response A null representation was received by the HTTPBridge, this cannot be converted to a suitable HTTP response..." both result and err are null.

I am pretty sure I am passing the arguments in correctly, but I am very new to node and trying to figure it out as I go.



UPDATE: Was able to get it working using strong-soap and a lot of help from a colleague

Here is the working code...

"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条回答 默认 最新

    报告相同问题?