将 PHP CURL 转换为 NPM CURL

Convert PHP CURL to NPM CURL

我目前正在尝试使用节点发出 CURL 请求。

我在 PHP 中有以下 CURL 请求:-

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://www.website.com');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_POSTFIELDS,  '');
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  'Authorization: token XXX',
  'Content-Type: application/json'
));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$output = curl_exec($ch);

curl_close($ch);

我需要 运行 在节点中执行相同的 CURL 请求。 我尝试了许多不同的 NPM 包并选择了 curljs

我有以下代码:-

var curl = require("curljs");

var curlOpts = curl.opts.connect_timeout(10);

curl("https://www.website.com -H Authorization: token XXX -H Content-Type: application/json --get", curlOpts, function(err, data, stderr) {
    console.log(err);
    console.log(data);
    console.log(stderr);
});

我能够从服务器获得响应而没有任何错误,但它没有返回任何数据。 我收到以下回复:-

null

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0

有人能帮助推动正确的方向吗?

如有任何帮助,我们将不胜感激。

另一个注意事项:-

我也试过用request

这是我的代码:-

var request = require('request');

var options = {
  url: 'https://www.website.com',
  headers: {
    'Authorization': 'token XXX',
    'Content-Type': 'application/json'
  }
};

function callback(error, response, body) {
  if (!error && response.statusCode == 200) {
    var info = JSON.parse(body);
    console.log(info);
  } else {
    console.log(body);
  }
}

request.get(options, callback);

但它一直返回错误。

使用node-fetch也returns同样的错误:-

fetch("https://www.website.com", {
  method: "GET",
  headers: {
    Authorization: "token XXX",
    "Content-Type": "application/json"
  }
}).then(function(response) {
  return response;
}).then(function(response) {
  console.log(response);
});

PHP 版本完美运行,我是否没有正确配置 headers?

HTTP 请求被烘焙到 Node.js。 httphttps 模块是一些核心模块:无需您执行任何其他操作即可安装它们。

但是,它们并不完全易于使用。它们本身并不复杂,但它们可以更简单。

我心目中最好的实现是 fetch API。这包含在现代网络浏览器中 (see MDN). You can also use it in Node by installing the node-fetch module:

npm install node-fetch --save

然后您可以使用 fetch 功能拨打电话。您可以使用 Promises.

以一种不错的现代 ES6 风格的方式获得结果

你的情况:

fetch("http://www.website.com", {
  method: "GET", // not strictly necessary
  headers: {
    Authorization: "token XXX",
    "Content-Type": "application/json" // is this accurate?!
  }
}).then(function(response) {
  return response.json(); // we want JSON output
}).then(function(response) {
  console.log(response); // logs the server response
});

如果你愿意使用现代箭头函数,你可以使它更漂亮。

已更新

我想你可以简单地使用 request

您的场景示例:

var request = require('request');

var options = {
    url: 'https://www.website.com',
    headers: {
        "Authorization": "token XXX",
        "Content-Type": "application/json"
    }
};

function callback(error, response, body) {
    if (!error && response.statusCode == 200) {
        console.log(response);
        console.log(body);
    }
}

request(options, callback);

该模块使用简单,并有直观的解决方案和示例。我们在整个 Node 项目中使用它已经有一段时间了,到目前为止还没有遇到任何问题。

希望对您有所帮助。