Psyduck - 可達鴨 之 鴨力山大
Current File : /home/irplbiz/public_html/demo_dev/demo_crm2/submit_form.php |
<?php
// Define SuiteCRM API endpoint
$suitecrm_url = 'https://orionedutech.co.in/suite/service/v4_1/rest.php';
// SuiteCRM API credentials (username, password, and the module to create the record in)
$api_user = 'manjari';
$api_pass = 'manjari';
$api_key = 'rgre6765sfdsf12df54$@'; // If needed, you can also authenticate using OAuth
// Get form data
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
// Authenticate and get the session ID (if using login authentication)
$auth = [
'user_auth' => [
'user_name' => $api_user,
'password' => md5($api_pass) // Password is MD5 encoded in SuiteCRM
],
'application_name' => 'CustomFormApp'
];
// Request for authentication (login) to SuiteCRM
$login_url = $suitecrm_url . '/login.php';
$login_data = json_encode($auth);
$ch = curl_init($login_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $login_data);
$response = curl_exec($ch);
$response_data = json_decode($response, true);
if ($response_data['id']) {
$session_id = $response_data['id']; // Session ID obtained after successful login
} else {
die("Login failed.");
}
// Create a new lead record (or a different module like Contact)
$lead_data = [
'name' => $first_name . ' ' . $last_name,
'first_name' => $first_name,
'last_name' => $last_name,
'email1' => $email,
'phone_work' => $phone,
'description' => $message
];
$lead_module = 'Leads';
$method = 'set_entry';
$fields = [
'module' => $lead_module,
'name_value_list' => $lead_data
];
// API request to create a new lead
$create_lead_url = $suitecrm_url;
$lead_data = [
'session' => $session_id,
'module_name' => $lead_module,
'name_value_list' => $fields['name_value_list']
];
curl_setopt($ch, CURLOPT_URL, $create_lead_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($lead_data));
$response = curl_exec($ch);
curl_close($ch);
// Parse the response
$response_data = json_decode($response, true);
if (isset($response_data['id'])) {
echo 'Lead created successfully with ID: ' . $response_data['id'];
} else {
echo 'Failed to create lead.';
}
?>