Updating values via CURL-API

Hello,

I am trying to update a value via CURL, put cannot make it working. Could you please check the attached code, and let me know, what I have to change:


In the Documentation, there is only a PHP-example on how to query via Curl, but not how to update. 

Thanks for your help.

Thanks Charles, for taking the time to answer.

I had to change from the http_build_query function to json_encode. This did the trick. Also, it seems to be enough for True/False values, to update the main field (e.g. field_315). The _raw field will updat automatically.

Final working code:  

	$ch2 = curl_init();
curl_setopt($ch2, CURLOPT_URL, "https://api.knackhq.com/v1/objects/object_6/records/".$id);

$fields = array("field_315" => "Yes");	
$data = json_encode($fields);

curl_setopt($ch2, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch2, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch2, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "X-Knack-Application-Id: " . $kn_appID, "X-Knack-REST-API-Key: " . $kn_APIKey)); 		
curl_setopt($ch2, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch2, CURLOPT_VERBOSE, true);


$returnVal = json_decode(curl_exec($ch2));&nbsp;</pre><p>&nbsp; Thanks again &amp; have a good week.</p><p>Cheers,<br>David</p>

Im not sure if you figured this out yet or not. It took me a while, but I got records to insert into my customers object finally. Updating worked for as well. 

I needed to use the full raw type array depth. so, 

 

$fields = array(
	$customerID => '',
	$customerCopmany => "Test Company",
	$customerName => array("last" => "Appleseed", "first" => "Johnny"),
	$customerBillingAddress => array("zip" => "27612", "state" => "NC", "city" => "Raleigh", "street" => "123 Abc St"),
	$customerEmail => array("email" => "email@testing.com"),
	$customerPassword => array("password" => "passwordABC123")
);
$data = json_encode($fields);

$ch2 = curl_init();
curl_setopt($ch2, CURLOPT_URL, “https://api.knackhq.com/v1/objects/".$customers."/records”);
curl_setopt($ch2, CURLOPT_CUSTOMREQUEST, ‘POST’);
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch2, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch2, CURLOPT_POSTFIELDS, $data );
curl_setopt($ch2, CURLOPT_HTTPHEADER, array(“Content-Type: application/json”, "Content-Lenght: ". strlen($data), "X-Knack-Application-Id: " . $kn_appID, "X-Knack-REST-API-Key: " . $kn_APIKey));
$postReturnVal = curl_exec($ch2);
curl_close($ch2);

 

Depending on what “field_315_raw” field type is. By changing this to PUT and removing everything but the address i was able to update that.

 

$fields = array(
$customerBillingAddress => array(“zip” => “5555”, “state” => “NC”, “city” => “BLAHBLAH”, “street” => “5555 ABC Dr.”)
);

$data = json_encode($fields);

$ch2 = curl_init();
curl_setopt($ch2, CURLOPT_URL, “https://api.knackhq.com/v1/objects/".$customers."/records"."/”.$id);
curl_setopt($ch2, CURLOPT_CUSTOMREQUEST, ‘PUT’);
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch2, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch2, CURLOPT_POSTFIELDS, $data );
curl_setopt($ch2, CURLOPT_HTTPHEADER, array(“Content-Type: application/json”, "Content-Lenght: ". strlen($data), "X-Knack-Application-Id: " . $kn_appID, "X-Knack-REST-API-Key: " . $kn_APIKey));
$postReturnVal = curl_exec($ch2);
curl_close($ch2);

 Hopefully that helps out you and anyone else with this issue.