File: /var/www/html/unicommerce-webhook/index.php
<?php
$log_time = date('Y-m-d h:i:sa');
$json = file_get_contents('php://input');
//$action = json_decode($json, true);
//$json = '{"chatId":"50ff97f0-628e-11ee-8e2f-d5b3ce0acad5","visitor":{"name":"Pramod Kumar","city":"pune","country":"IN","email":"pramod@cccinfotech.com"},"message":{"sender":{"type":"visitor"},"text":"Name : Pramod Kumar\r\nPhone : 121212121212\r\nOfficial Email : pramod@cccinfotech.com\r\nCompany : UNI\r\nSelect the team you would like to chat : Sales","type":"msg"},"time":"2023-10-04T08:18:25.220Z","event":"chat:start","property":{"id":"61b0861bc82c976b71c05add","name":"Unicommerce eSolutions"}}';
wh_log($json);
// wh_log('************** Start Log For Day : ' . $log_time . '**********');
// wh_log($json);
// wh_log('************** END Log For Day : ' . $log_time . '**********');
function wh_log($json)
{
$APIdata = json_decode($json, true);
$APIdata_visitor = isset($APIdata['visitor']) ? $APIdata['visitor'] : '';
$user_name = isset($APIdata_visitor['name']) ? $APIdata_visitor['name'] : '';
$user_email = isset($APIdata_visitor['email']) ? $APIdata_visitor['email'] : '';
$user_country = isset($APIdata_visitor['country']) ? $APIdata_visitor['country'] : '';
$user_city = isset($APIdata_visitor['city']) ? $APIdata_visitor['city'] : '';
$APIdata_message = isset($APIdata['message']) ? $APIdata['message'] : '';
$APIdata_message_text = isset($APIdata_message['text']) ? $APIdata_message['text'] : '';
$message_data = explode(':', $APIdata_message_text);
$user_phone = '';
$user_company = '';
$user_chat = '';
if(count($message_data) > 0)
{
$user_phone = isset($message_data[2]) ? $message_data[2] : "";
if($user_phone)
{
$user_phone = str_replace(" Official Email","",$user_phone);
$user_phone = str_replace("Official Email","",$user_phone);
}
$user_company = isset($message_data[4]) ? $message_data[4] : "";
if($user_company)
{
$user_company = str_replace(" Company","",$user_company);
$user_company = str_replace("Company","",$user_company);
}
$user_chat = isset($message_data[5]) ? $message_data[5] : "";
if($user_chat)
{
$user_chat = str_replace(" chat","",$user_chat);
$user_chat = str_replace("chat","",$user_chat);
}
}
$APIdata_message_text = isset($APIdata_message['text']) ? $APIdata_message['text'] : '';
$pdata = "encoding=UTF-8&callback=jQuery36405827080259442845_1696342387304&last_name=".$user_name."&phone=".$user_phone."&email=".$user_email."&company=".$user_company."&00N6F00000YeASK=Less%20than%201000&oid=00D90000000skwX&LeadSource=Chat&00N6F00000YeASA=&00N6F00000YeAS0=&00N6F00000YeAS5=&00N6F00000YeASF=&00N6F00000YeARv=1696342387305";
$ch = curl_init(); // Initiate cURL
$url = "https://webto.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8"; // Where you want to post data
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POST, true); // Tell cURL you want to post something
curl_setopt($ch, CURLOPT_POSTFIELDS, $pdata); // Define what you want to post
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Return the output in string format
$output = curl_exec ($ch); // Execute
// var_dump($output); // Show output
$apiresponse = '';
if(curl_errno($ch)) {
$apiresponse = 'Error: ' . curl_error($ch);
} else {
$apiresponse = $output;
}
curl_close ($ch); // Close cURL handle
$log_filename = 'log';
if (!file_exists($log_filename))
{
// create directory/folder uploads.
mkdir($log_filename, 0777, true);
}
$api_response = '';
$api_response .= $json;
$api_response .= '\n';
$api_response .= 'PostData = '.$pdata;
$api_response .= '\n';
$api_response .= "API Response = ".$apiresponse;
$log_file_data = $log_filename.'/log_' . date('d-M-Y') . '.log';
$path = dirname(__FILE__).'/'.$log_file_data;
file_put_contents($path, $api_response . '\n', FILE_APPEND);
}