简体   繁体   中英

Converting a PHP cURL request into node.js

I'm looking at implementing Summry , and they only give PHP API connection examples - is there any chance someone could turn it into a JS request for me?

I essentially just want it to parse the text I give it, nothing super fancy yet.

I've tried to see what CURLOPT_POSTFIELDS , and CURLOPT_HTTPHEADER match to in a JS request, to no avail. I'm probably looking in the wrong place, however.

PHP example

$long_article = "Long article text goes here";

$ch = curl_init("http://api.smmry.com/&SM_API_KEY=XXXXXXXXX&SM_LENGTH=14&SM_WITH_BREAK");
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Expect:")); // Important do not remove
curl_setopt($ch, CURLOPT_POST, true); 
curl_setopt($ch, CURLOPT_POSTFIELDS, "sm_api_input=".$long_article);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
$return = json_decode(curl_exec($ch), true);
curl_close($ch);

JavaScript replication

...
const request = require('request-promise');
...

const long_article = 'Long article text goes here';
const r = request({
  method: 'POST',
  uri: `http://api.smmry.com/&SM_API_KEY=${process.env.SMMRY_API_KEY}`,
  // headers: { Authorization: 'array(Expect:)' },
  // body: { sm_api_input: long_article },
  json: true,
});

request(r)
.then((parsedBody) => { debug(parsedBody); })
.catch((err) => { debug(err); });

The error I'm getting is { sm_api_error: 1, sm_api_message: 'INSUFFICIENT VARIABLES' } so I'm atleast hitting the right URL - so that's a start :)

Thanks in advance!

Ollie

The uri that you are passing to uri is attempting to use a Query String but you're not correctly formatting the URI you're requesting. A Query String should start with a ? and separate Key/Value pairs with a & . Your Query String is beginning with a & instead of a ?

Change:
http://api.smmry.com/&SM_API_KEY=${process.env.SMMRY_API_KEY}&SM_LENGTH=14 &SM_WITH_BREAK

To:
http://api.smmry.com/?SM_API_KEY=${process.env.SMMRY_API_KEY}&SM_LENGTH=14&SM_WITH_BREAK

Additionally, you're creating a request r and then passing that request to request . That's not how the request-promise module works. When invoking request(opts) , the request to the URI will be made immediately and the corresponding Promise will be returned. Once the request is complete any chained handlers will be invoked ( .then() , .catch() , .finally() ).

const request = require('request-promise')
const sm_api_input = 'Long article text goes here'

request({
    method: 'POST',
    headers: {
        'Expect': '100-continue'
    },
    uri: `http://api.smmry.com/?SM_API_KEY=${process.env.SMMRY_API_KEY}&SM_LENGTH=14&SM_WITH_BREAK`,
    form: {sm_api_input},
    json: true,
    timeout: 20000
})
  .then(body => debug)
  .catch(err => debug)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM