<?php 
include('../includes/config/config.php');
include('../log_entry.php');

 $params = array(
                'username'  => 'staff',
				'password'  => 'staff!123'
            );
$token_Url ="https://uat3.factohr.com/staff/api/ACL/AuthenticateUserEmployeeAndGetUserToken";
$data_url ="https://uat3.factohr.com/staff/API/Employee/getEmployeeDetailsByModifiedOnDate";
// A function that will make a GET request to the /campaigns endpoint

function get_data_from_api() {

// Run the function that will make a POST request and return the token

$exoclick_token = get_token_from_api();

$new_token = $exoclick_token->Token;

$auth_array = array(
        "Authorization:",
        "Bearer",
        $new_token
);

$new_token = implode(" ", $auth_array);

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => $data_url,
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "GET",
  CURLOPT_POSTFIELDS => "",
  CURLOPT_HTTPHEADER => array(
     $new_token,
     "Content-Type: application/json",
     "cache-control: no-cache"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

$data = json_decode($response, true);

// do something with the data

print_r($data);

}


function get_token_from_api() {

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => $token_Url,
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => $params, //"{'username': 'staff','password':'staff!123'}",
  CURLOPT_HTTPHEADER => array(
    "Accept: */*",
    "Cache-Control: no-cache",
    "Connection: keep-alive",
    "Content-Type: application/json",
  ),
));

$response = curl_exec($curl);
error_log("Response:".$response);
$err = curl_error($curl);
error_log("Error fetching Token:".$err);

curl_close($curl);

// Decode the response from the API

    $decoded_response_object = json_decode($response);
	error_log("decoded_response_object:".$decoded_response_object);

    curl_close($curl);

// Return the decoded response so you can use it to make another request
    return $decoded_response_object;

}

// Run the initial function
get_data_from_api();


/*
$api = new RestClient([
    'base_url' => "https://uat3.factohr.com/staff/api/ACL/AuthenticateUserEmployeeAndGetUserToken", 
    'format' => "json", 
     // https://dev.twitter.com/docs/auth/application-only-auth
    'headers' => ['Authorization' => 'Bearer '.OAUTH_BEARER], 
	'parameters' => ['username' => 'staff ', 'password'=>'staff!123' ], 
]);
$result = $api->get("token", ['q' => "#php"]);
$response = json_decode($result);
echo $response;

// GET http://api.twitter.com/1.1/search/tweets.json?q=%23php
//if($result->info->http_code == 200)
  //  var_dump($result->decode_response());


	
/*$complaintId = $_REQUEST['complaintId'];
$query = "select * from complaints where complaint_id = '".$complaintId."' ";
  if (!$result = @mysqli_query($conn,$query)) {
            die(mysqli_error($conn));
        }
	$data = array();
	if(mysqli_num_rows($result) > 0) {
        while ($row = mysqli_fetch_assoc($result)) {
            $data = $row;
        }
    }
	 else
    {
        $data['status'] = 200;
        $data['message'] = "Data not found!";
    }
 echo json_encode($data);*/
?>