【发布时间】:2021-05-03 05:15:40
【问题描述】:
我想使用 curl 发送数据数组。
$email = $_SESSION['useremail'];
$exam_code = $this->input->post('exam_code');
$bank_question = ['3343', '3345', '3333'];
$student_answer = ['B', 'A', 'C'];
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://url',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => '{
"email_student" : ' . $email . ',
"exam_code" : ' . $exam_code . ',
"bank_question_id" : ' . $bank_question . ',
"student_answer" : ' . $student_answer . '
}',
CURLOPT_HTTPHEADER => array(
'x-api-key: ............',
'Cookie: ci_session=.......'
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
但是当我像这样发送数据数组 $bank_question 和 $students_answer 时,它总是错误 Array to string conversion 。我应该怎么做才能发送数据数组?
【问题讨论】:
-
永远不要尝试通过字符串连接来构建 JSON。