Tôi đang xử lý yêu cầu cURL khi người dùng gửi biểu mẫu hiện có để gửi một số dữ liệu đến dịch vụ bên ngoài.
Tôi đã thêm chức năng này với dòng này:
$form['actions'][$action]['#submit'][] = 'my_module_push_data';
Và các yêu cầu cURL của tôi:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://resturl.com&id=' . $id . '&secret=' . $secret);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close($ch);
$result = json_decode($result, true);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://resturl.com/submit.json?access_token=' . $result['access_token']);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($formData));
curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Kiểu nội dung:application/json'));
curl_setopt($ch, CURLOPT_RETURNTTRASFER, TRUE);
$server_output = curl_exec($ch);
$server_output = json_decode($server_output);
Với mã này, tôi gặp lỗi này:
Uncaught PHP Ngoại lệ LogicException: "Không thể đặt lỗi biểu mẫu sau khi xác thực biểu mẫu kết thúc." tại /mnt/gfs/.../.../docroot/core/lib/Drupal/Core/Form/FormState.php
Tôi đã đi từng dòng qua yêu cầu cURL này và dòng tôi đã đánh dấu (thứ hai curl_exec()
gây ra lỗi này. Nếu tôi bỏ qua dòng đó thì nó hoạt động tốt. đầu tiên curl_exec()
hoạt động tốt và tôi đã xác minh rằng nó đang trả lại mã thông báo truy cập.
Tôi làm gì sai ở đây? Cảm ơn bạn!
CHỈNH SỬA: Chức năng đầy đủ (làm rối một số chi tiết)
function my_module_service_signup(array $form, \Drupal\Core\Form\FormStateInterface $form_state) {
$vals = $form_state->getValues();
$formData = [];
$myVals = [];
$clientId = Cài đặt::get('service_api_id');
$clientSecret = Cài đặt::get('service_api_secret');
$accountId = 'abc123456';
$formData['unique_id'] = '1234';
$myVals = [
'công ty' => $vals['field_company'][0]['value'],
'firstName' => $vals['field_first_name'][0]['value'],
'lastName' => $vals['field_last_name'][0]['value'],
...
];
$formData['input'][0]['fields'] = $myVals;
cố gắng {
$getClient = \Drupal::httpClient();
$request = $getClient->post('https://' . $accountId . '.resturl.com/get/token?my_id=' . $clientId . '&my_secret=' . $clientSecret);
$response = json_decode($request->getBody());
} bắt(RequestException $e) {
}
}