Community
Showing results for 
Search instead for 
Do you mean 
Reply

Adding An Activity

New Member
Posts: 7
Country: New_Zealand

Adding An Activity

 

I am trying to add an activity using PHP Curl.

 

I have got the bearer token back and I can use it to get lists of activities etc, so basic operations appear to be fine.

 

Attempting to add an activity results in an exception "Activity Must Involve atleast One Contact"

 

The json payload I'm sending, has at least one contact.

 

The json is I would imagine is supposed to be sent in the body of the request, and thus I'm adding it to CURLOPT_POSTFIELDS like this:

curl_setopt($ch, CURLOPT_POSTFIELDS, $json);

 

Is this correct or am I missing something?

 

My Json looks like this:

 

{"startTime":"2016-01-15T16:00:00.000+00:00","endTime":"2016-01-15T17:30:00.000+00:00","location":"The Pub","isTimeless":false,"isPrivate":false,"activityPriorityName":"Very Important","activityTypeName":"Drinking Beer","details":"Get the beers in","subject":"Friday Beers","recurSpec":[],"contacts":[{"id":"6d38c7c1-9792-40e7-a6d5-9fac68ff0a39","displayName":"John Smith"},{"id":"6d38c7c1-9792-40e7-a6d5-9fac68ff0a39","displayName":"Joh"}],"created":"2016-01-14T11:12:00.000+00:00","edited":"2016-01-14T11:12:00.000+00:00","scheduledBy":"Bob Smith","scheduledFor":"Bob Smith"}

 

which I build from a PHP array and json_encode()

New Member
Posts: 7
Country: New_Zealand

Re: Adding An Activity

I figured it out

 

The documentation doesn't explicitly state that you need to send an appropriate content type header. Also the format of the json refers to a returned activity, which is why it includes the id. Worth noting that CURL will set the content type to multipart/form-data if you feed CURLOPT_POSTFIELDS an array.

 

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'http://'.$HOST.'/act.web.api/api/Activities');

curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);

$headers = array(
'Authorization: Bearer ' . $token,
'Content-type: application/json'
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

 

$addActivity = array(
"startTime" => "2016-01-29T16:00:00.000+00:00",
"endTime" => "2016-01-29T17:30:00.000+00:00",
"location" => "Bedrock",
"isTimeless" => false,
"isPrivate" => false,
"activityPriorityName" => "Very Important",
"activityTypeName" => "Drinking Beer",
"details" => "Get the beers in",
"subject" => "Friday Beers",
"contacts" => array(
array(
"id" => "6d38c7c1-9792-40e7-a6d5-9fac68ff0a39",
"displayName" => "Fred Flintstone"
),
array(
"id" => "6d38c7c1-9792-40e7-a6d5-9fac68ff0a39",
"displayName" => "Barney Rubble"
)
),
"created" => "2016-01-14T11:12:00.000+00:00",
"edited" => "2016-01-14T11:12:00.000+00:00",
"scheduledBy" => "Wilma Flintstone",
"scheduledFor" => "Betty RUbble"
);

 

$json = json_encode($addActivity);

curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
$ret = curl_exec($ch);