[英]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? 我正在实现Summry ,并且它们仅提供PHP API连接示例-有人可以将其转换为JS请求吗?
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. 我试着看什么CURLOPT_POSTFIELDS
,并CURLOPT_HTTPHEADER
匹配的JS请求,但没有成功。 I'm probably looking in the wrong place, however. 但是,我可能在错误的位置。
PHP example PHP示例
$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 JavaScript复制
...
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 :) 我收到的错误是{ sm_api_error: 1, sm_api_message: 'INSUFFICIENT VARIABLES' }
所以我至少会找到正确的URL-所以这是一个开始:)
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. 您传递给uri
尝试使用查询字符串,但您没有正确格式化所请求的URI。 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
. 另外,您要创建一个请求r
,然后将该请求传递给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. 调用request(opts)
,将立即向URI发出请求,并将返回相应的Promise。 Once the request is complete any chained handlers will be invoked ( .then()
, .catch()
, .finally()
). 请求完成后,将调用所有链接的处理程序.then()
.catch()
.finally()
.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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.