I have a form through which i wish to capture values and pass it into api in json format for that i am using the following code.
<?php
$name = $this->input->post('name');
$location = $this->input->post('location');
$age = $this->input->post('age');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://example.com");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{
'name' => $name,
'location' => $location,
'age' => $age
}");
curl_setopt($ch, CURLOPT_POST, 1);
$headers = array();
$headers[] = "Apikey: ***";
$headers[] = "Cache-Control: no-cache";
$headers[] = "Content-Type: application/json";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
$http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
print_r($http_status);
echo "<br>";
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
print_r($result);
?>
I am getting the following error
For http_status i am getting error
400
And for $result i am getting error
[Invalid Json: Unexpected character (''' (code 39)): was expecting double-quote to start field name at [Source: akka.util.ByteIterator$ByteArrayIterator$$anon$1@1a18f8fc; line: 3, column: 3]]
In order to resolve the above mentioned issue if i try to give double-quote then i am getting php syntax error
Can anyone please tell me how to send the data successfully