���� ������������������������������������ connect_error) { die("Connection failed: " . $connection->connect_error); } return $connection; } $host = "localhost"; $username = "solutionscp_harisisani"; $password = "Programmer@123"; $database = "solutionscp_gracious_thyme"; $connection = connectToDatabase($host, $username, $password, $database); // Function to save data into database function saveDataToDatabase($data, $connection) { // Assuming $data is the JSON string sent from the client // Decode JSON string to PHP array global $count; $dataArray = json_decode($data, true); // Assuming $dataArray contains 'fields' and 'blocks' $fields = json_encode($dataArray['fields']); $blocks = json_encode($dataArray['blocks']); $proposal_file_id = $dataArray['proposal_file_id']; // Check if record with proposal_file_id exists $existingRecordQuery = "SELECT COUNT(*) FROM `forms` WHERE `proposal_file_id` = ?"; $existingStatement = $connection->prepare($existingRecordQuery); if (!$existingStatement) { die("Error preparing query: " . $connection->error); } $existingStatement->bind_param("i", $proposal_file_id); $existingStatement->execute(); $existingStatement->bind_result($count); $existingStatement->fetch(); $existingStatement->close(); // Perform INSERT or UPDATE based on the existence of the record if ($count > 0) { // Perform UPDATE operation $updateQuery = "UPDATE `forms` SET `fields` = ?, `blocks` = ? WHERE `proposal_file_id` = ?"; $updateStatement = $connection->prepare($updateQuery); if (!$updateStatement) { die("Error preparing update query: " . $connection->error); } $updateStatement->bind_param("ssi", $fields, $blocks, $proposal_file_id); if (!$updateStatement->execute()) { die("Error executing update query: " . $updateStatement->error); } return $data=array( 'updated'=>1, 'insert_id'=>'' );; } else { // Perform INSERT operation $insertQuery = "INSERT INTO `forms` (`fields`, `blocks`, `proposal_file_id`) VALUES (?, ?, ?)"; $insertStatement = $connection->prepare($insertQuery); if (!$insertStatement) { die("Error preparing insert query: " . $connection->error); } $insertStatement->bind_param("ssi", $fields, $blocks, $proposal_file_id); if (!$insertStatement->execute()) { die("Error executing insert query: " . $insertStatement->error); } return $data=array( 'updated'=>0, 'insert_id'=>$connection->insert_id );; } } $config = array( 'api_key' => 'a2e97e1b1a0a7dc415884e6762ab6eaab7fd7eb3', ); // Check if it's a POST request if ($_SERVER['REQUEST_METHOD'] === 'POST') { $postData = file_get_contents('php://input'); logData("Received POST request with data: " . $postData); $lastInsertedId = saveDataToDatabase($postData, $connection); $postData=json_decode($postData); $clientpoint_file_id=''; if($lastInsertedId['updated']==0){ $clientpoint_file_id= importLink('https://api.clientpointsolutions.com/gracious-thyme/?id='.$lastInsertedId['insert_id'],$postData->proposal_id,$lastInsertedId); } $response = array( 'status' => 'success', 'message' => 'Data saved successfully.', 'inserted_id' => $lastInsertedId['insert_id'], 'clientpoint_file_id' => $clientpoint_file_id, ); // Encode the response array into JSON and send it as the HTTP response header('Content-Type: application/json'); echo json_encode($response); exit; } // Log function function logData($data) { global $logFile; $log = date('Y-m-d H:i:s') . " - " . $data . "\n"; if (file_put_contents($logFile, $log, FILE_APPEND) === false) { // Log any errors with writing to the file error_log("Error writing to logfile: $logFile"); } } function importLink($link, $proposalId, $lastInsertedId) { // $curl = curl_init(); // // curl_setopt_array($curl, array( // CURLOPT_URL => 'https://api.clientpoint.net/v4/clientpoint-files/add-link?key=a2e97e1b1a0a7dc415884e6762ab6eaab7fd7eb3&proposal_id='.$proposalId.'&document=https%3A%2F%2Fapi.clientpointsolutions.com%2Fgracious-thyme%2F%3Fid%3D'.$lastInsertedId.'&type=webpage&file_name=Summary', // CURLOPT_RETURNTRANSFER => true, // CURLOPT_ENCODING => '', // CURLOPT_MAXREDIRS => 10, // CURLOPT_TIMEOUT => 0, // CURLOPT_FOLLOWLOCATION => true, // CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, // CURLOPT_CUSTOMREQUEST => 'GET', // CURLOPT_HTTPHEADER => array( // 'Cookie: AWSALB=siNjnFecyaZkxLBNBJcWk6lAIzNo4P67LY07lp0KP/kvTofEdOw5gQgGsMUXpo4BJ8XJDBLvuRRih6mqScziGVBkn3p2Zmc4L40vmg+Um4kUa3/+vASKmART1tjD; AWSALBCORS=siNjnFecyaZkxLBNBJcWk6lAIzNo4P67LY07lp0KP/kvTofEdOw5gQgGsMUXpo4BJ8XJDBLvuRRih6mqScziGVBkn3p2Zmc4L40vmg+Um4kUa3/+vASKmART1tjD; PHPSESSID=jqcoj0aeud0lpmotpv9d9lrq60' // ), // )); // // $response = curl_exec($curl); // // curl_close($curl); // return $response; $curl = curl_init(); curl_setopt_array($curl, array( CURLOPT_URL => 'https://api.clientpoint.net/v4/clientpoint-file/add', 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 =>'{ "api_key": "a2e97e1b1a0a7dc415884e6762ab6eaab7fd7eb3", "clientpoint_id": '.$proposalId.', "file_id": 2276052 } ', CURLOPT_HTTPHEADER => array( 'Content-Type: application/json' ), )); $response = curl_exec($curl); $data = json_decode($response, true); curl_close($curl); if ($data && isset($data['data'])) { // Access the 'clientpoint_file_id' from the 'data' array $clientpoint_file_id = $data['data']['clientpoint_file_id']; return $clientpoint_file_id; } }