<?php
session_start();
error_reporting(0);
require_once("db.php");
$logPath = "log/kenya/safaricom/gettoken_".date("Ymd").".txt";
error_log("called url: ".date('Ymd His').": ".$_SERVER['REQUEST_URI']."\n", 3, $logPath);

$ch = curl_init();
//curl_setopt($ch, CURLOPT_URL, 'https://dtsvc.safaricom.com:8480/api/auth/login');
curl_setopt($ch, CURLOPT_URL, 'https://dsvc2.safaricom.com:9480/api/auth/login');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
//curl_setopt($ch, CURLOPT_POSTFIELDS, "{\n   \"password\": \"Password@1234\", \n   \"username\": \"tamaituk_api\" \n }");
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\n   \"password\": \"DialRepAPI@ps3503\", \n   \"username\": \"DialRepAPI\" \n }");
$headers = array();
$headers[] = 'Content-Type: application/json';
$headers[] = 'Accept: application/json';
$headers[] = 'X-Requested-With: XMLHttpRequest';
$headers[] = 'X-Authorization: Bearer';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
//echo $response;
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}
curl_close($ch);

///////////////////////////////////////////////////////////////////////////////////////////////////////////

if(empty($response))
{
	error_log(date('Ymd His').": response is empty\n", 3, $logPath);
	$keycheck=1;
	while($keycheck==1)
	{
		/////////////////////////////
		$ch = curl_init();
//curl_setopt($ch, CURLOPT_URL, 'https://dtsvc.safaricom.com:8480/api/auth/login');
curl_setopt($ch, CURLOPT_URL, 'https://dsvc2.safaricom.com:9480/api/auth/login');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
//curl_setopt($ch, CURLOPT_POSTFIELDS, "{\n   \"password\": \"Password@1234\", \n   \"username\": \"tamaituk_api\" \n }");
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\n   \"password\": \"DialRepAPI@ps3503\", \n   \"username\": \"DialRepAPI\" \n }");
$headers = array();
$headers[] = 'Content-Type: application/json';
$headers[] = 'Accept: application/json';
$headers[] = 'X-Requested-With: XMLHttpRequest';
$headers[] = 'X-Authorization: Bearer';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
//echo $response;
if (curl_errno($ch)) {
    //echo 'Error:' . curl_error($ch);
}
curl_close($ch);
error_log(" Response inside if :".date('Ymd His').": " .$response."\n", 3, $logPath);
if(empty($response))
{
	error_log(date('Ymd His').": again empty response \n", 3, $logPath);	
}
else
{
error_log(date('Ymd His').": increasing keycheck \n", 3, $logPath);	
$keycheck=2;	
}
error_log(date('Ymd His').": keycheck=$keycheck\n", 3, $logPath);
		/////////////////////////////
	}
	
}







///////////////////////////////////////////////////////////////////////////////////////////////////////////

$data = json_decode($response,true);
$arr = (object) $data;

error_log(" Response:".date('Ymd His').": " .$response."\n", 3, $logPath);
error_log(" token:".date('Ymd His').": " .$arr->token. "\n", 3, $logPath);
echo $arr->token;

$Query="insert into tbl_access_token_gamingbox_enscke  (entrydate,token,msg,refreshtoken) values (now(),'".$arr->token."','".$arr->msg."','".$arr->refreshToken."')";
	error_log("Query:".date('Ymd His').": ". $Query."\n", 3, $logPath);
	
		if(!mysqli_query($con1,$Query))
		  {	
			error_log("Error description:".date('Ymd His').": ". mysqli_error($con1)."\n", 3, $logPath);	
		  }


		  $Query1="insert into tokenforkenya (lastupdatedate,msisdn,OfferCode,statuss) values (now(),'".$arr->token."','".$arr->msg."','".$arr->refreshToken."')";
	error_log("Query:".date('Ymd His').": ". $Query1."\n", 3, $logPath);
	
		if(!mysqli_query($con1,$Query1))
		  {	
			error_log("Error description:".date('Ymd His').": ". mysqli_error($con1)."\n", 3, $logPath);	
		  }


?>

