Matt Cremeens Matt Cremeens - 4 years ago 164
JSON Question

Parsing JSON Data Sent from Calendly.com

I am trying to send data sent by calendly.com to my webserver. I am connecting to that webserver and can post data to it just fine using

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $myvars);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$response = curl_exec($ch);

$myvars = 'first_name=' . $full_name . '&email=' . $thisemail;


However, I am having difficulty getting data into
$myvars
. I have tried this

$data = $_REQUEST['payload'];
$unescaped_data = stripslashes($data);
$obj = json_decode($unescaped_data);
$thisemail = $obj->invitee->email;
$full_name = $obj->invitee->name;


But that variable doesn't populate. I have also changed the

$data = $_REQUEST['payload']


to

$data = $_POST['payload']


I have also tried a totally different approach

$json = file_get_contents('php://input');
$request = json_decode($json, true);
$thisemail = $request["payload"]["invitee"]["email"];


When I just put in dummy string in
$thisemail
and
$full_name
it works as expected.

I also read that this data is
gzip
encoded. So I did also try to change this

$request = json_decode($json, true);


to this

$request = gzdecode($json);


but also with no effect.

Here is a snippet of what the data is supposed to be looking like

{
"event":"invitee.created",
"time":"2016-08-23T19:16:01Z",
"payload":{
"event_type":{
"kind":"1-on-1",
"slug":"event_type_name",
"name":"Event Type Name",
"duration":15
},
"event":{
"uuid":"BBBBBBBBBBBBBBBB",
"assigned_to":[
"Jane Sample Data"
],
"extended_assigned_to": [
"name": "Jane Sample Data",
"email": "user@example.com",
"primary": false
],
"start_time":"2016-08-23T12:00:00Z",
"start_time_pretty":"12:00pm - Tuesday, August 23, 2016",
"invitee_start_time":"2016-08-23T12:00:00Z",
"invitee_start_time_pretty":"12:00pm - Tuesday, August 23, 2016",
"end_time":"2016-08-23T12:15:00Z",
"end_time_pretty":"12:15pm - Tuesday, August 23, 2016",
"invitee_end_time":"2016-08-23T12:15:00Z",
"invitee_end_time_pretty":"12:15pm - Tuesday, August 23, 2016",
"created_at":"2016-08-23T00:00:00Z",
"location":"The Coffee Shop",
"canceled":false,
"canceler_name":null,
"cancel_reason":null,
"canceled_at":null
},
"invitee":{
"uuid":"AAAAAAAAAAAAAAAA",
"first_name":"Joe",
"last_name":"Sample Data",
"name":"Joe Sample Data",
"email":"not.a.real.email@example.com",

...

Answer Source

@blewherself's answer brings to light the important fact that the JSON in the example found here is not valid JSON and I have upvoted their answer for that reason.

After further investigation, however, I do believe that this is an error of that page alone and that the actual JSON passed by them is not invalid. However, I am happy to add here my answer that I found after much persistence as I believe that the documentation at calendly.com is too brief to be very useful.

The JSON file can be retrieved with $HTTP_RAW_POST_DATA and otherwise you can access the individual components more or less according to the example in the docs, at least the part I was interested in. Like so

$data = json_decode($HTTP_RAW_POST_DATA, true);
$full_name = $data["payload"]["invitee"]["name"];
$thisemail = $data["payload"]["invitee"]["email"];
$first_name = explode(" ", $full_name)[0];
$last_name = explode(" ", $full_name)[1];

You'll notice a glaring omission that their form asks customers: phone number. It appears that even though the client is asked for it, it is not passed to your url in the JSON file. Also, though first and last name are given as a passed item in the JSON example in the docs, neither it nor many other items in the example are asked for in their form. I am not sure how they expect that data to be passed in their JSON. I sincerely hope the folks at calendly address these issues soon.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download