【问题标题】:Array to string conversion when calling array调用数组时数组到字符串的转换
【发布时间】: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 。我应该怎么做才能发送数据数组?

【问题讨论】:

标签: php arrays


【解决方案1】:

您可以像这样对您的数据进行 JSON 编码:

    $aData = [
    'email_student' => $email,
    'exam_code' => $exam_code,
    'bank_question_id' => $bank_question,
    'student_answer' => $student_answer,
];

CURLOPT_POSTFIELDS => json_encode($aData);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-05-22
    • 2017-10-27
    • 2015-02-21
    • 1970-01-01
    相关资源
    最近更新 更多