将 API 调用从 PHP 和 cURL 转换为 ColdFusion cfhttp



我正在尝试对在线测试公司的API调用进行编码。 他们提供了一个 PHP 和 cURL 的示例调用,我需要使用<CFHTTP>在 ColdFusion 11 中实现该调用。 到目前为止,我的尝试失败了。 我从他们的服务器/API得到的唯一响应是:

状态代码 ="连接失败。状态代码不可用。">

错误详细信息 ="I/O 异常:远程主机在握手期间关闭连接"。

如果它有效,我会得到一个详细说明计算分数的 JSON 字符串。 请注意,在下面的代码中,出于安全原因,我更改了一些值,除此之外,它是原始代码。 任何建议或意见将是 非常感谢,谢谢。

以下是 ColdFusion/cfhttp 代码:

<cfoutput>
<cfset sdata = [
{
"customerid" = "ACompany",
"studentid" = "test",
"form" = "X",
"age" = "18.10",
"norms" = "grade",
"grade" = "2"
},
{
"scores" = [
{"subtest"="math","score"="34"},
{"score"="23","subtest"="lang"},
{"score"="402","subtest"="rcomp"}
]
}
]>
<!--- create JSON string for request --->
<cfset jsdata = serializeJSON(sdata)>
<!--- make the call --->
<cfhttp method="Get" url="https://www.APIwebsite.php" timeout="10" result="varx">
<cfhttpparam type="header" name="Content-Type" value = "application/json; charset=utf-8"/>
<cfhttpparam type="body" value = "#jsdata#"/>
<cfhttpparam type="header" name="Authorization" value="AuthCode"/> 
<cfhttpparam type="header" name="Content-Length" value = "#len(jsdata)#"/>
</cfhttp>
<!--- show results --->
cfhttp return status code: [#varx.statusCode#]<br> 
cfhttp return fileContent: [#varx.fileContent#]<br>
</cfoutput>

以下是 PHP/cURL 代码:

<?php
$data = array
(
"customerid" => "ACompany",
"studentid" => "test",
"scoringtype" => 2,
"form" => "X",
"age" => "18.10",
"norms" => 'grade',
"grade" => '2',
"scores" => array(
array("subtest" => "math", "score" => "34"),
array("subtest" => "lang", "score" => "23"),
array("subtest" => "rcomp", "score" => "402")
));
$url = 'https://www.APIwebsite.php';
$json_string = json_encode($data);
$headers = array (
"Content-Type: application/json; charset=utf-8",
"Content-Length: " .strlen($json_string),
"Authorization: AuthCode"
);
$channel = curl_init($url);
curl_setopt($channel, CURLOPT_RETURNTRANSFER, true);
curl_setopt($channel, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($channel, CURLOPT_HTTPHEADER, $headers);
curl_setopt($channel, CURLOPT_POSTFIELDS, $json_string);
curl_setopt($channel, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($channel, CURLOPT_CONNECTTIMEOUT, 10);
$response = curl_exec($channel); // execute the request
$statusCode = curl_getInfo($channel, CURLINFO_HTTP_CODE);
$error = curl_error($channel);
curl_close($channel);
http_response_code($statusCode);
if ( $statusCode != 200 ){
echo "Status code: {$statusCode} n".$error;
} else {
$data = json_decode($response,true);
foreach ($data as $key => $value) {
echo nl2br($key . ': ' . $value . "n");
}
}
?>

首先确保您提供的网址正确无误(我知道这是示例,但.php不是有效的域名扩展名(,并且SSL证书有效

如果两者都正确,则应将请求方法更改为POST,以便通过正文发送 json 数据

根据 http 语义

客户端不应在 GET 请求中生成正文。有效载荷 在GET请求中收到的没有定义的语义,无法更改 请求的含义或目标,并可能导致一些实现 拒绝请求并关闭连接,因为它 可能作为请求走私攻击

cfhttp 中有一个字符集参数,因此您无需在标头中发送它

这是一个应该可以工作的代码

<cfset sdata = [
{
"customerid" = "ACompany",
"studentid" = "test",
"form" = "X",
"age" = "18.10",
"scoringtype" = 2,
"norms" = "grade",
"grade" = "2"
},
{
"scores" = [
{"subtest"="math","score"="34"},
{"score"="23","subtest"="lang"},
{"score"="402","subtest"="rcomp"}
]
}
]>
<!--- create JSON string for request --->
<cfset jsdata = serializeJSON(sdata)>
<!--- make the call --->
<cfhttp method="post" charset="utf-8" url="https://api.website.com/" timeout="10" result="varx">
<cfhttpparam type="header" name="Content-Type" value="application/json"/>
<cfhttpparam type="header" name="Authorization" value="AuthCode"/> 
<cfhttpparam type="header" name="Content-Length" value="#len(jsdata)#"/>
<cfhttpparam type="body" value="#jsdata#"/>
</cfhttp>
<!--- show results --->
<cfoutput>
cfhttp return status code: [#varx.statusCode#]<br>
cfhttp return fileContent: [#varx.fileContent#]<br>
</cfoutput>

最新更新