<?php
//error_reporting(E_ALL);
include("../db.php");
$ID = $LinksDetails["sms_id"];
$Pwd = $LinksDetails["sms_pwd"];
$senderID = trim($LinksDetails["senderID"]);

$TemplateID = $_REQUEST["templateid"];
$EntityID = $_REQUEST["entityid"];

if ($TemplateID == "") {
    $TemplateID = $LinksDetails["templateid"];
}
if ($EntityID == "") {
    $EntityID = $LinksDetails["entityid"];
}

$jsondata = $_POST['json'];
$idata = json_decode($jsondata, true);

$msg = $idata['str'];
$mobile = $idata['mobile'];
$session = $idata['session'];
$page = $idata['page'];
$school_id = $idata['school_id'];
$Added_by = $idata['Added_by'];

/*
 $msg=$_REQUEST['str'];
 $mobile=$_REQUEST['mobile'];
 
 */
//  	  $msg=mysql_real_escape_string($msg);
// 	  $mobile=mysql_real_escape_string($mobile);

//  $response = file_get_contents("https://www.businesssms.co.in/sms.aspx?Id=$ID&Pwd=$Pwd&PhNo=$mobile&text=".urlencode($msg));

// echo "https://www.businesssms.co.in/sms.aspx?Id=$ID&Pwd=$Pwd&PhNo=$mobile&text=".urlencode($msg).'"';
// print_r($response);
// echo $response;

//Prepare you post parameters

$postData = array('Id' => $ID, 'Pwd' => $Pwd, 'PhNo' => $mobile, 'text' => $msg);

//API URL

//http://sms.mevite.in/api.php?username=$username&password=$pwd&sender=$sender&sendto=$phone&message=$msg"
//$url="https://www.businesssms.co.in/sms.aspx";

$encodedMsg = urlencode($msg);
$url = "http://sms.uphargroup.com/submitsms.jsp?user=$ID&key=$Pwd&mobile=91$mobile&message=$encodedMsg&senderid=$senderID&accusage=1&entityid=$EntityID&tempid=$TemplateID";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);

//curl_close($ch);

// init the resource
//$ch = curl_init();
//curl_setopt_array($ch, array(
//    CURLOPT_URL => $url,
//    CURLOPT_RETURNTRANSFER => true,
//    CURLOPT_POST => true,
//    CURLOPT_POSTFIELDS => $postData
//    //,CURLOPT_FOLLOWLOCATION => true
//));

//Ignore SSL certificate verification
//curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
//curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

//get response
//$response = curl_exec($ch);

// echo $response;
// print_r($response);

if ($response) {

    $aryData = array("mobile" => $mobile, "msg" => $msg, "status" => $response, "session" => $session, "dtime" => $date, "page" => $page, "school_id" => $school_id, "added_by" => $Added_by);

    $insert_complain = $db->insertAry("sms", $aryData);

    if ($insert_complain > 0) echo json_encode(array('status' => 1, 'msg' => 'message send..!', 'url' => $url));

} else {
    echo json_encode(array('status' => 0, 'msg' => 'message not send..!', 'url' => $url, 'curl_res' => $response));
}