API PUT produces unexpected result
As part of our first operational use of the API, I need to update two fields dynamically. For trials, I am using php cUrl, modeled on your sample application. I have no problems with GET, but PUT leaves me baffled.
Each time I run the script, the target fields are not changed - but the membership of the target contact goes from "active" to "membership not allocated".
Clearly my code is doing this: but how/why? Can anyone give me a clue as to what is happening?
Also, my debugging script shows a result JSON containing all the target contact's data. Is this as it should be? And can it be prevented (unnecessary data traffic, etc.)?
My cUrl code is (sorry about the bad formatting below):
$accountUrl = "https://api.wildapricot.org/v2/accounts/[account]/Contacts/";
$queryParams = array(
'Id' => $contact,
'Field values' => array(
0 => array(
'FieldName' => "fieldname1",
'Value' => 'XXXXX'
),
1 => array(
'FieldName' => "fieldname2",
'Value' => 'XXXXXX'
)
)
);
$url = $accountUrl.$contact;
$verb = "PUT";
$ch = curlinit();
$headers = array(
'Authorization: Bearer ' . $token,
'Content-Type: application/json'
);
curlsetopt($ch, CURLOPTURL, 'https://api.wildapricot.org/v2/accounts/[account]/Contacts/[contact]');
curlsetopt($ch, CURLOPTHTTPHEADER, $headers);
curlsetopt($ch, CURLOPTCUSTOMREQUEST, $verb);
if ($queryParams) {
$jsonData = jsonencode($queryParams);
curlsetopt($ch, CURLOPTPOSTFIELDS, $jsonData);
}
curlsetopt($ch, CURLOPTRETURNTRANSFER, true);
$jsonResult = curlexec($ch);
if ( !$jsonResult ) {
echo curlerrno($ch) . ': ' . curlerror($ch);
}
curlclose($ch);
Fixed in v5.1.5
-
Hamish McCallum commented
Excellent news: thanks for your help.
-
Evgeny Zaritovskiy commented
This will be fixed in the next system updated - v5.1.5 - in several days.
-
Evgeny Zaritovskiy commented
I'll forward this information to our developers, thanks for the data.
-
Hamish McCallum commented
Evgeny
This json marks the target contact's membership as not assigned:
[data_json] => {"Id":"5814141","FieldValues":[{"FieldName":"Trial1","Value":"goatboat"},{"FieldName":"Trial2","Value":"gibberish"}]}
and this one leaves the membership in place:
[data_json] => {"Id":"5814141","MembershipEnabled":"true","FieldValues":[{"FieldName":"Trial1","Value":"pigeon"},{"FieldName":"Trial2","Value":"guelph"}]}
Obviously, if I have to confirm that a membership is enabled, then there'll be two API calls: a GET request, to find out what the enabled status is, and then the PUT.
-
Evgeny Zaritovskiy commented
Can you post the actual JSON you send to server?
-
Hamish McCallum commented
OK, some progress: changed code slightly and corrected "Field values" typo to "FieldValues". Target field values are now being updated, which is very good!
And I've worked out that setting CURLOPT_RETURNTRANSFER as false stops the unwanted data dump.
Unfortunately, the target contact's membership is still being un-assigned each time I run the script.
The code now looks like this: