We are trying to work with the Sharpspring API to synch up some of the data we have on their system with our local database. They have some example code in PHP (which I'm only vaguely familiar with) but I would like to convert it to Coldfusion since that's the language I know much better. The requests also need to be submitted in JSON format (which I'm very new at).
I've been successful getting their "getleads" method to work in Coldfusion (so I know it can be done), however, I haven't been able to get their "updateleads" method to work in Coldfusion, more than likely because I have the structure of the JSON messed up. Here's the error I get:
{"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":[]}},{"success":false,"error":{"code":208,"message":"Object is malformed (expected array, got scalar)","data":[]}}]},"error":[{"code":208,"message":"Object is malformed (expected array, got scalar)","data":[]},{"code":208,"message":"Object is malformed (expected array, got scalar)","data":[]},{"code":208,"message":"Object is malformed (expected array, got scalar)","data":[]}],"id":"04E511DD-A430-D9B8-367D78679476F6A6","callCount":"2","queryLimit":"50000"}
From their documentation, the structure of the JSON for that method should be this (but note that the Lead ID is the record ID and the Request ID appears to be any unique ID so I send a GUID):
{
"method":"updateLeads",
"params":
{"objects":
[
{"id":"<lead ID>","firstName":"fooUpdate","lastName":"barUpdate"}
]
},
"id":"<your request ID>"
}
Here is what I'm trying to do in Coldfusion to accomplish that:
<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>
Does anyone have any suggestions on how I need to tweak that code to get the correct JSON structure that the API is requiring? Any help would be appreciated!
For reference, here is the PHP example code (for getting the lead, not updating one like I need) that Sharpspring provides if that helps....
<?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;
?>