我正在尝试将API调用编码为在线测试公司。他们提供了PHP和cURL的示例调用,我需要使用<CFHTTP>
在ColdFusion 11中实现该调用。到目前为止,我的尝试失败了。我从他们的服务器/API得到的唯一响应是:
和
如果工作正常,我将获得一个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 semantics
cfhttp中有一个charset参数,因此您无需在 header 中发送它
这是应该起作用的代码
<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>
关于php - 将API调用从PHP和cURL转换为ColdFusion cfhttp,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59038209/