我们正在尝试使用 Sharpspring API 将我们在他们系统上的一些数据与我们的本地数据库同步。他们在 PHP 中有一些示例代码(我只是模糊地熟悉),但我想将其转换为 Coldfusion,因为这是我更了解的语言。请求还需要以 JSON 格式提交(我对此很陌生)。
我已经成功地让他们的“getleads”方法在 Coldfusion 中工作(所以我知道可以做到),但是,我无法让他们的“updateleads”方法在 Coldfusion 中工作,很可能是因为我JSON的结构搞砸了。这是我得到的错误:
{"result":{"updates":[{"success":false,"error":{"code":208,"message":"Object is malformed (expected array, got scalar)","data": []}},{"success":false,"error":{"code":208,"message":"Object is malformed (expected array, got scalar)","data":[]}},{ “成功”:假,“错误”:{“代码”:208,“消息”:“对象格式错误(预期的数组,得到标量)”,“数据”:[]}}]},“错误”:[ {"code":208,"message":"Object is malformed (expected array, got scalar)","data":[]},{"code":208,"message":"Object is malformed (expected array , 得到标量)","数据":[]},{"code":208,"message":"对象格式错误(预期数组,得到标量)","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;
?>