nodejs与PHP curl代码的等价物是什么
这可能看起来很愚蠢,但我正在尝试从API(WHMCS)获取一些数据。在文档中,他们有如下代码:nodejs与PHP curl代码的等价物是什么,php,node.js,curl,axios,Php,Node.js,Curl,Axios,这可能看起来很愚蠢,但我正在尝试从API(WHMCS)获取一些数据。在文档中,他们有如下代码: // Call the API $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $whmcsUrl . 'includes/api.php'); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_TIMEOUT, 30); curl_setopt($ch, CURLOPT_
// Call the API
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $whmcsUrl . 'includes/api.php');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postfields));
$response = curl_exec($ch);
if (curl_error($ch)) {
die('Unable to connect: ' . curl_errno($ch) . ' - ' . curl_error($ch));
}
curl_close($ch);
// Decode response
$jsonData = json_decode($response, true);
// Dump array structure for inspection
var_dump($jsonData);
const axios = require('axios')
const qs = require('qs')
const data = qs.stringify(postFieldsObject)
const config = {
method: 'post',
url: `${whmcsUrl}includes/api.php`,
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
data : data
}
axios(config)
.then(function (response) {
console.log(JSON.stringify(response.data))
})
.catch(function (error) {
console.log(error)
})
我已经使用axios在nodejs中编写了代码,目的是做同样的事情:
axios.get(whmcsUrl + `includes/api.php?accesskey=${apiAccessKey}`, postFields)
.then(res => console.log(res))
.catch(err => console.log(err))
对PHP或Node都没有深入的了解,请帮助!当我在节点中执行此请求时,我得到了一个禁止的403
?我做错了什么
更新:我现在不是传递对象(postFields
),而是在url本身中传递诸如username或pw之类的内容:
axios.post(whmcsUrl + `includes/api.php?action=${apiAction}&username=${apiIdentifier}&password=${apiSecret}&accesskey=${apiAccessKey}&responsetype=json`)
.then(res => console.log(res))
.catch(err => console.log(err))
它仍然给我带来了
403禁止的错误。axios相当于CURLOPT\u POSTFIELDS,http\u build\u query($POSTFIELDS)
有两个方面-首先将参数字符串化并将其作为帖子主体传递,然后在标题中指定application/x-www-form-urlcoded
作为内容类型
大概是这样的:
// Call the API
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $whmcsUrl . 'includes/api.php');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postfields));
$response = curl_exec($ch);
if (curl_error($ch)) {
die('Unable to connect: ' . curl_errno($ch) . ' - ' . curl_error($ch));
}
curl_close($ch);
// Decode response
$jsonData = json_decode($response, true);
// Dump array structure for inspection
var_dump($jsonData);
const axios = require('axios')
const qs = require('qs')
const data = qs.stringify(postFieldsObject)
const config = {
method: 'post',
url: `${whmcsUrl}includes/api.php`,
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
data : data
}
axios(config)
.then(function (response) {
console.log(JSON.stringify(response.data))
})
.catch(function (error) {
console.log(error)
})
axios相当于CURLOPT\u POSTFIELDS,http\u build\u query($POSTFIELDS)
,它有两个功能:首先将参数字符串化,并将其作为帖子正文传递,然后在标题中指定application/x-www-form-urlencoded
作为内容类型
大概是这样的:
// Call the API
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $whmcsUrl . 'includes/api.php');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postfields));
$response = curl_exec($ch);
if (curl_error($ch)) {
die('Unable to connect: ' . curl_errno($ch) . ' - ' . curl_error($ch));
}
curl_close($ch);
// Decode response
$jsonData = json_decode($response, true);
// Dump array structure for inspection
var_dump($jsonData);
const axios = require('axios')
const qs = require('qs')
const data = qs.stringify(postFieldsObject)
const config = {
method: 'post',
url: `${whmcsUrl}includes/api.php`,
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
data : data
}
axios(config)
.then(function (response) {
console.log(JSON.stringify(response.data))
})
.catch(function (error) {
console.log(error)
})
你是在发邮件还是发邮件?对于axios,您正在执行GET,但PHP正在执行POST@AndrewNolan我在get和post通话中都遇到了同样的错误你是在做get还是post?对于axios,您正在执行GET,但PHP正在执行POST@AndrewNolan我在get和post CallsHanks中都遇到了相同的错误!在没有qs
软件包的情况下是否可以执行此操作?我不再使用node了。是的'qs.stringify'。您也可以轻松地手工构建它。关键是这个字符串是帖子数据,不是URL的一部分,其次必须传递正确的内容类型标题。谢谢!在没有qs
软件包的情况下是否可以执行此操作?我不再使用node了。是的'qs.stringify'。您也可以轻松地手工构建它。关键点在于,该字符串是POST数据,而不是URL的一部分,其次,必须传递正确的内容类型头。