如何解决Sharpspring JSON 格式 - PHP 示例转换为 Coldfusion
我们正在尝试使用 Sharpspring API 将我们在他们系统上的一些数据与我们的本地数据库同步。他们在 PHP 中有一些示例代码(我只是模糊地熟悉),但我想将其转换为 Coldfusion,因为这是我更了解的语言。请求还需要以 JSON 格式提交(我对此很陌生)。
我已经成功地让他们的“getleads”方法在 Coldfusion 中工作(所以我知道它可以完成),但是,我一直无法让他们的“updateleads”方法在 Coldfusion 中工作,超过可能是因为我搞砸了 JSON 的结构。这是我得到的错误:
{"result":{"updates":[{"success":false,"error":{"code":208,"message":"对象格式错误(预期数组,得到标量) ","data":[]}},{"success":false,"message":"对象格式错误(预期数组,得到标量)","data": []}},"data":[]}}]},"error":[{"code":208,"data":[]},{"code":208,"data":[] }],"id":"04E511DD-A430-D9B8-367D78679476F6A6","callCount":"2","queryLimit":"50000"}
从他们的文档来看,该方法的 JSON 结构应该是这样的(但请注意,潜在客户 ID 是记录 ID,请求 ID 似乎是任何唯一 ID,因此我发送了一个 GUID):
{
"method":"updateLeads","params":
{"objects":
[
{"id":"<lead ID>","firstName":"fooUpdate","lastName":"barUpdate"}
]
},"id":"<your request ID>"
}
这是我在 Coldfusion 中尝试做的事情:
<cfscript>
variables.datafields = {};
variables.datafields['id'] = CreateUUID(); // The API requires an ID for each request
variables.datafields['method'] = "updateLeads";
variables.datafields['id'] = "123456789"; // The ID of the record you want to update
variables.datafields['firstName'] = "John";
variables.datafields['lastName'] = "Smith";
variables.datafields = serializejson(variables.datafields);
writeoutput(datafields); // Write the JSON to see what's submitted
</cfscript>
<cfhttp url="http://<the sharpspring API URL>" method="POST" result="resultName">
<cfhttpparam name="accountID" type="url" value="<Our Account ID>">
<cfhttpparam name="secretKey" type="url" value="<Our Account Key>">
<cfhttpparam type="header" name="Content-Type" value="application/json" />
<cfhttpparam type="body" value="#variables.datafields#" />
</cfhttp>
<!--- display the API return values --->
<br><br>
<cfoutput>
#resultName.filecontent#
</cfoutput>
是否有人对我需要如何调整该代码以获得 API 所需的正确 JSON 结构有任何建议?我们将不胜感激!
作为参考,这里是 Sharpspring 提供的 PHP 示例代码(用于获得领先,而不是更新我需要的),如果有帮助的话....
<?PHP
/** Get all leads with a limit of 500 results */
$limit = 500;
$offset = 0;
$method = 'getLeads';
$params = array('where' => array(),'limit' => $limit,'offset' => $offset);
$requestID = session_id();
$accountID = '<account-id>';
$secretKey = '<secret-key>';
$data = array(
'method' => $method,'params' => $params,'id' => $requestID,);
$queryString = http_build_query(array('accountID' => $accountID,'secretKey' => $secretKey));
$url = "http://api.sharpspring.com/pubapi/v1/?$queryString";
$data = json_encode($data);
$ch = curl_init($url);
curl_setopt($ch,CURLOPT_CUSTomrEQUEST,"POST");
curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_HTTPHEADER,array(
'Content-Type: application/json','Content-Length: ' . strlen($data),'Expect: '
));
$result = curl_exec($ch);
curl_close($ch);
echo $result;
?>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。