<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
//$message = urlencode($_GET["message"]);
$message = $_GET["message"];
$mask = urlencode($_GET["mask"]);
$number = urlencode($_GET["number"]);
$smsID = urlencode($_GET["messageId"]);
$Uname = urlencode($_GET["Uname"]);
$Upass = urlencode($_GET["Upass"]);
$api_unicode= urlencode($_GET['api_unicode']);
$tablesname= urlencode($_GET['tablesname']);
$filedname= urlencode($_GET['filedname']);

//echo $message ; 
//echo '<br>';
//echo $_GET["message"] ;

//die ; 

error_reporting(E_ALL);
//echo 'im here '; die ; 
$currentdatetime = date("Y:m:d H:i:s");
// ----------------------------
$curl = curl_init();
//echo 'https://'.$Uname.'/sms/3/messages'  ; 
//echo '<br>';
//echo $Upass ;


//https%3A%2F%2Fppnlg3.api-pk2.infobip.com/sms/3/messages
//https://ppnlg3.api-pk2.infobip.com/sms/3/messages
 
//echo '<br>';
//App 175ff49f9815c99e0c97d8c5c4baa3f7-ce0631b6-d484-4dd9-8779-4e22848f7a0d
//App 175ff49f9815c99e0c97d8c5c4baa3f7-ce0631b6-d484-4dd9-8779-4e22848f7a0d


// ppnlg3.api-pk2.infobip.com 
curl_setopt_array($curl, array(
    CURLOPT_URL => 'https://'.$Uname.'/sms/3/messages',
    //CURLOPT_URL => 'https://ppnlg3.api-pk2.infobip.com/sms/3/messages',
	//CURLOPT_URL => ''.$Uname.'/sms/3/messages',
    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 =>'{"messages":[{"sender":"'.$mask.'","destinations":[{"to":"'.$number.'"}],"content":{"text":"'.$message.'"}}]}',
    CURLOPT_HTTPHEADER => array(
        'Authorization: '.' App '.$Upass.'', // Replace with your actual token App 175ff49f9815c99e0c97d8c5c4baa3f7-ce0631b6-d484-4dd9-8779-4e22848f7a0d
        'Content-Type: application/json',
        'Accept: application/json'
    ),
));

$response = curl_exec($curl);

if(curl_errno($curl)){
 $result = curl_error($curl); //die ;//
}
//echo  ' fhdjfh ghdf'.$response ;  die ; 
//

$result =  $response; ; 
//echo  ' fhdjfh ghdf'.$result ;  die ; 
// ----------------------------
include_once("config.php");
//$sqlUpdateSMSa="UPDATE ".$tablesname." SET ".$filedname."='resa',response_time='".$currentdatetime."' where id='$smsID'";
//$queryUpdateSMSa=mysqli_query($connect,$sqlUpdateSMSa);
$currentdatetimeb = date("Y:m:d H:i:s");
$sqlUpdateSMSb="UPDATE ".$tablesname." SET ".$filedname."='".urlencode($result)."',response_time='".$currentdatetimeb."' where id='$smsID'";
//echo $sqlUpdateSMSb ; 
$queryUpdateSMSb=mysqli_query($connect,$sqlUpdateSMSb);
$errors = curl_error($curl);
if(!$result){
    //die("Connection Failure");
    $result =  curl_error($curl) ; 
    $currentdatetimec = date("Y:m:d H:i:s");
$sqlUpdateSMSc="UPDATE ".$tablesname." SET ".$filedname."='".urlencode($errors)."',response_time='".$currentdatetimec."' where id='$smsID'";
//echo $sqlUpdateSMSc ; 
$queryUpdateSMSc=mysqli_query($connect,$sqlUpdateSMSc);
    
}
if(strpos($result, 'Promotional') !== false){
  // echo 'true'; 
$stopQueryskh = "select   customer , message_id  from ".$tablesname."  where id='$smsID'";
//echo $stopQueryskh ;
$start_que_skh = mysqli_query($connect,$stopQueryskh);
$cData = mysqli_fetch_array($start_que_skh);
//echo 'hghg hghgh ___ '.$cData['customer'] ;
$customerid = $cData['customer'] ; 

$sqlUpdateSMSc="UPDATE sentmessages_".$customerid." SET error  ='Promotional message is blocked as per customer instructions' where id='".$cData['message_id']."'";
//echo $sqlUpdateSMSc ; 
$queryUpdateSMSc=mysqli_query($connect,$sqlUpdateSMSc);
}

  curl_close($curl);
?>