Matt Cremeens Matt Cremeens - 4 years ago 164
JSON Question

Parsing JSON Data Sent from

I am trying to send data sent by 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
. 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']


$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
it works as expected.

I also read that this data is
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

"name":"Event Type Name",
"Jane Sample Data"
"extended_assigned_to": [
"name": "Jane Sample Data",
"email": "",
"primary": false
"start_time_pretty":"12:00pm - Tuesday, August 23, 2016",
"invitee_start_time_pretty":"12:00pm - Tuesday, August 23, 2016",
"end_time_pretty":"12:15pm - Tuesday, August 23, 2016",
"invitee_end_time_pretty":"12:15pm - Tuesday, August 23, 2016",
"location":"The Coffee Shop",
"last_name":"Sample Data",
"name":"Joe Sample Data",


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 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