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

标签 php curl coldfusion cfhttp

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

Statuscode = "Connection Failure. Status code unavailable. "

ErrorDetail = "I/O Exception: Remote host closed connection during handshake".

如果它正常工作,我会得到一个详细说明计算分数的 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,以便通过body发送json数据

根据http semantics

A client should not generate a body in a GET request. A payload received in a GET request has no defined semantics, cannot alter the meaning or target of the request, and might lead some implementations to reject the request and close the connection because of its potential as a request smuggling attack

cfhttp中有一个字符集参数,因此不需要在 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/

相关文章:

PHP 插入数组值、表名

rest - 使用 Github API 禁用拉取请求上的合并按钮并使用 REST 重新启用它

powershell - 在引号中的字符串中转义@

json - ColdFusion > 2016. SerializeJSON 保留具有重复项的顺序

eclipse - 如何使用 cfeclipse 插件在 Eclipse 中格式化文本

coldfusion - SpreadsheetFormatRows 格式颜色 ColdFusion

php - 如何使用php内置的http服务器+mysql?

php - 登录系统sqlserver连接错误

php - 我什么时候应该转义 sql 查询

php - 为 fabpot/goutte 客户端设置 CURL 参数