<?php 

if($gen==1){
	// generate code 
	$from=date("Y-m-d G:i:s");
	 $date_from=date("Y-m-d G:i:s");
require_once "HTTP/Request.php";
include('library/Requests.php');	 
		 
		 
	$URL = 'http://ns4.mit.bz/PalLandingPageAPI/sms/sub/generateCode'; 
	  
	$req =new HTTP_Request("");
	$req->setURL($URL);
	$req->setMethod("POST");
	$req->addHeader("Content-Type", "application/x-www-form-urlencoded"
	 );
	 
	 
	 
	 
	// echo(substr($RowOperatorPrefex['operatorPrefix_pref'],-2)." -- ".$phone." -- ".$RowOperatorPrefex['ServicesOperator_sid']);
		   $req->addPostData("operatorCode",$RowOperatorPrefex['operatorPrefix_code']);
		     $req->addPostData("msisdn",$phone);
			  $req->addPostData("serviceId",$RowOperatorPrefex['ServicesOperator_sid']);
// exit(substr($RowOperatorPrefex['operatorPrefix_pref'],-2)." -- ".$phone."  -- ".$RowOperatorPrefex['ServicesOperator_sid']);
	
	 $response = $req->sendRequest();
	 $responseBody = $req->getResponseBody();
	  

$data = json_decode($responseBody,true);
 //print_r($data);
 // exit();
 $to=date("Y-m-d G:i:s");
 $isertAPISLogs=mysqli_query($connect,"insert into providersLogsAPI set 
 providersLogsAPI_subid='".$subscriberID."',providersLogsAPI_APIresponse='".$data['code']."'
 ,providersLogsAPI_apitype='g'
 ,providersLogsAPI_phone='".$phone."'
 ,providersLogsAPI_from='".$from."'
 ,providersLogsAPI_to='".$to."'
 ,providersLogsAPI_apiResult='".$data['result']."'
 ");

//exit();
	
	
}


if($sub==1){
	
 $from=date("Y-m-d G:i:s");
//	subscribe 
	
	$date_from=date("Y-m-d G:i:s");
require_once "HTTP/Request.php";
include('library/Requests.php');	 


	$URL = 'http://ns4.mit.bz/PalLandingPageAPI/sms/sub/subMsisdn'; 
	  
	$req =new HTTP_Request("");
	$req->setURL($URL);
	$req->setMethod("POST");
	$req->addHeader("Content-Type", "application/x-www-form-urlencoded"
	 );
	 
	//  exit(substr($serviceOperatorD['operatorPrefix_pref'],-2)." -- ".post($connect,$_REQUEST['vkey'])." -- ".$serviceOperatorD['ServicesOperator_sid']." -- ".$row['SubscribersLogs_phone']);
		   $req->addPostData("operatorCode",$serviceOperatorD['operatorPrefix_code']);
		     $req->addPostData("code",post($connect,$_REQUEST['vkey']));
			  $req->addPostData("serviceId",$serviceOperatorD['ServicesOperator_sid']);
			   $req->addPostData("msisdn",$row['SubscribersLogs_phone']);
	
	
	 $response = $req->sendRequest();
	 $responseBody = $req->getResponseBody();
	  

$data = json_decode($responseBody,true);
 
  $to=date("Y-m-d G:i:s");
 
$isertAPISLogs=mysqli_query($connect,"insert into providersLogsAPI set 
 providersLogsAPI_subid='".$row['SubscribersLogs_id']."',providersLogsAPI_APIresponse='".$data['code']."'
 ,providersLogsAPI_apitype='s'
 ,providersLogsAPI_phone='".$row['SubscribersLogs_phone']."'
 ,providersLogsAPI_from='".$from."'
 ,providersLogsAPI_to='".$to."'
 ,providersLogsAPI_apiResult='".$data['result']."'
  ,providersLogsAPI_key='".post($connect,$_REQUEST['vkey'])."'
 ");
 
 

$resultReturn=$data['code'];
 
if($data['code']=='-4'  || $data['result']=='Not Valid Code'){
	$resultReturn =11; // invalid code  
	
	}
	
	
	
	
	if($data['code']=='1' && $data['result']!='Not Valid Code'){ // faild 
	 
	if($data['result']=='Jawwal API Error Response '
	 || $data['result']=='already sub'
	 ){ //echo("vvvvvv");
		  session_regenerate_id() ;
		 
		 }
		 
	 $resultReturn ='-1';
	 
	 
	 
	 
	 
	 
	}
	
	
	
	if($row['SubscribersLogs_phone']=='0599473070'){
		//exit($resultReturn);
		
		}
	
	
}