0
我有一个表单,我希望捕获值并将其传递到json格式的api中,以便使用以下代码。无法通过使用curl将表单中的动态数据传递给API
<?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);
?>
我收到以下错误
对于HTTP_STATUS我收到错误
400
并为$结果我得到错误
[Invalid Json: Unexpected character (''' (code 39)): was expecting double-quote to start field name at [Source: [email protected]; line: 3, column: 3]]
为了解决上面提到的问题,如果我尝试给双引号,然后我得到PHP语法错误
任何人都可以请告诉我如何成功发送数据
我收到此错误:此异常已记录ID为73jbj3e8b。 – sammy