<?php
error_reporting(E_ALL);
$message = urlencode($_GET["message"]);
$mask = urlencode($_GET["mask"]);
$number = $_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']);


if(strpos($number, ",") !== FALSE )
		{ // bulk here 
	
$Uname = $_GET["Uname"];
$Upass = $_GET["Upass"];

	// --------------- bulk code strat 
	
	
	$Date = date("Y-m-d H:i:s", strtotime('+11 hours'));
            $date2 =  date("n/j/Y h:i:s a", strtotime($Date ) );        
            ini_set("soap.wsdl_cache_enabled", 0);
            //https://cbs.zong.com.pk/ReachCWSv2/CorporateSmsV2.svc
            //http://cbs.zong.com.pk/ReachCWSv2/CorporateSmsV2.svc?xsd=xsd0
            $url         = 'http://cbs.zong.com.pk/ReachCWSv2/CorporateSmsV2.svc?wsdl';
            
            $client     = new SoapClient($url, array("trace" => 1, "exception" => 0)); 
            $resultBulkSMS = $client->BulkSmsv2(
                    array('objBulkSms' => 
                        array(  'LoginId'=>$Uname, //here type your account name
                                'LoginPassword'=>$Upass, //here type your password

                                'Mask'=>$mask, //here set allowed mask against your account or you will get invalid mask
                                'Message'=>$message,
                                'UniCode'=>$api_unicode,
                                'CampaignName'=>uniqid(),
                                'CampaignDate'=>$date2, // data from where sms will start sending, if not sure type current date in m/d/y hh:mm:ss tt format.
                                'ShortCodePrefered'=>'n',
                                'NumberCsv'=>$number)));

//print_r($resultBulkSMS->BulkSmsv2Result ); die ; 

                //return 'ddddd=='. die ; 
                $status = $resultBulkSMS->BulkSmsv2Result;
                        //$status .="";
                if(strpos($status, "Successfully") !== FALSE ){
                            $result['sent'] = "Message Sent Successfully";
                            //$result['status'] = "1";
                        }else{
                            $result['status'] = $status; 
                //return 2; die;

                }

        $response = json_encode($result);
	
	
	// end bulk code
	
include("config.php");
$currentdatetime = date("Y:m:d H:i:s");
//include("config.php");
$sqlUpdateSMS="UPDATE ".$tablesname." SET ".$filedname."='".urlencode($response)."',response_time='".$currentdatetime."' where id='$smsID'";
//echo $sqlUpdateSMS ; 
$queryUpdateSMS=mysqli_query($connect,$sqlUpdateSMS);

		    
}else{ // sigle
    $postfields = "loginId=".$Uname."&loginPassword=".$Upass."&mask=".$mask."&Destination=".$number."&Message=".$message."&uniCode=".$api_unicode."&ShortCodePrefered=n";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://cbs.zong.com.pk/reachrestapi/home/SendQuickSMS');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
if(!$result){
   $result =  $errors ;
    //die("Connection Failure");
    
}
// also get the error and response code
$errors = curl_error($ch);
//$response = curl_getinfo($ch, CURLINFO_HTTP_CODE);


   curl_close($ch);

include("config.php");
$currentdatetime = date("Y:m:d H:i:s");
//include("config.php");
$sqlUpdateSMS="UPDATE ".$tablesname." SET ".$filedname."='".urlencode($result)."',response_time='".$currentdatetime."' where id='$smsID'";
//echo $sqlUpdateSMS ; 
$queryUpdateSMS=mysqli_query($connect,$sqlUpdateSMS);


} // end sigle 


?>