Psyduck - 可達鴨 之 鴨力山大

Current Path : home/irplbiz/public_html/demo_dev/demo_crm/
Upload File :
Current File : /home/irplbiz/public_html/demo_dev/demo_crm/submit_form.php

<?php

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // Collect form data
    $first_name = $_POST['first_name'];
    $last_name = $_POST['last_name'];
    $email = $_POST['email'];

    // SuiteCRM API credentials
    $crm_url = "https://orionedutech.co.in/suite/service/v4_1/rest.php";
	$crm_username = "niladri_oes";
	$crm_password = "niladri_oes";

    // Log in to the SuiteCRM API
    $login_parameters = [
        'user_auth' => [
            'user_name' => $username,
            'password' => md5($password)  // Password is MD5 hashed
			 //"version" => "1"
        ],
        'application_name' => 'OrionCRM',
    ];

    $login_payload = [
        'method' => 'login',
        'input_type' => 'JSON',
        'response_type' => 'JSON',
        'rest_data' => json_encode($login_parameters),
    ];

    $response = make_api_request($url, $login_payload);
    $session_id = $response['id'];  // Store the session ID from login response

    // Create a new lead
    $lead_data = [
        'first_name' => $first_name,
        'last_name' => $last_name,
        'email1' => $email,
    ];

    $create_parameters = [
        'session' => $session_id,
        'module_name' => 'Leads',  // The module you're inserting data into
        'name_value_list' => $lead_data,
    ];

    $create_payload = [
        'method' => 'set_entry',
        'input_type' => 'JSON',
        'response_type' => 'JSON',
        'rest_data' => json_encode($create_parameters),
    ];

    $create_response = make_api_request($url, $create_payload);

    if (isset($create_response['id'])) {
        echo "Lead created successfully!";
    } else {
        echo "Error creating lead!";
    }

    // Logout after processing
    $logout_parameters = [
        'session' => $session_id,
    ];

    $logout_payload = [
        'method' => 'logout',
        'input_type' => 'JSON',
        'response_type' => 'JSON',
        'rest_data' => json_encode($logout_parameters),
    ];

    make_api_request($url, $logout_payload);
}

function make_api_request($url, $payload) {
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($payload));
    curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/x-www-form-urlencoded']);

    $response = curl_exec($ch);
    curl_close($ch);

    return json_decode($response, true);
}
?>