I have a following request method below which I am posting to an API via Curl
{
"status": [
{
"status": "string",
"date": "string"
}
],
"first_name": "string",
"last_name": "string"
}
The first_name and last_name values get posted successfully but status and date parameters submitted empty values. How do I also get the value of status and date parameters to be submitted also?
Here is the code:
<?php
$tok ='my token goes here';
$params= array(
'first_name' => "nancy",
'last_name' => "moree",
'status' => 'active',
'date' => '2020-12-29'
);
$url ='https://app.drchrono.com/api';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Authorization: Bearer $tok"));
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$output = curl_exec($ch);
echo $output;
Advertisement
Answer
As described by the JSON requirement:
"status": [
{
"status": "string",
"date": "string"
}
]
Your actual status need to be an object inside another array key status:
$params= array(
'first_name' => "nancy",
'last_name' => "moree",
'status' => array(
array('status' => 'active', 'date' => '2020-12-29')
)
);