<?php
$data___ = array();
$sql__     = $pdo->query("SELECT * FROM tb_listing_maps WHERE  phone != '' AND send_whatsapp = 'checked' AND sent = 'N'");
while($data__    = $sql__->fetch(PDO::FETCH_ASSOC))
{
    $data___[] = ''.$data__['phone'].'|'.$data__['title'].'';
}
$nomor_hp = implode(',', $data___);
?>

<?php
$category = $module;
$sql     = $pdo->query("SELECT * FROM tb_whatsapp_skedul WHERE category = '".$category."'");
$data    = $sql->fetch(PDO::FETCH_OBJ);
$scedule = $data->tanggal_skedul . ' '. $data->jam_skedul;

?>

<?php
$sql_s     = $pdo->query("SELECT * FROM tb_setting");
$data_s    = $sql_s->fetch(PDO::FETCH_OBJ);
?>

<?php

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => 'https://api.fonnte.com/send',
  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 => array(
'target' => ''.$nomor_hp.'',
'message' => $data->whatsapp_message,
'delay' => '0',
'schedule' => strtotime($scedule), //time
'countryCode' => '62', //optional
),
  CURLOPT_HTTPHEADER => array(
    'Authorization: ' . $data_s->api_key . '' //change TOKEN to your actual token
  ),
));

$response = curl_exec($curl);

curl_close($curl);

$data = json_decode($response,true);

echo "<br>Detail: ". $data['detail']; 
echo "<br>Status: ". $data['process']; 

$pdo->query(
	"UPDATE tb_listing_maps SET 
	    send_date            ='".$scedule."',
	    sent                 = 'Y'
	    WHERE 
	    send_whatsapp ='checked' AND sent = 'N'"
		);	
		

/*for($x=0; $x < count($data['target']); $x++)
{
    echo $data['target'][$x];
    echo ", ";
}*/
?>