File: /var/www/html/wp-content/themes/ormedia/qrpatrol/api3.php.bak
<?php
require_once 'formr/class.formr.php';
require "libs/HTML_Object.php";
class HtmlTable{
function genTable($data = array())
{
$rows = array();
foreach ($data as $row) {
$cells = array();
foreach ($row as $cell) {
$cells[] = "<td>{$cell}</td>";
}
$rows[] = "<tr>" . implode('', $cells) . "</tr>";
}
return "<table class='hci-table'>" . implode('', $rows) . "</table>";
}
function HtmlTable(){
}
}
header("Content-Encoding: UTF-8");
?>
<?php
// Defines HTML_Object() Class
$objHTML = new HTML_Object();
$objHTMLDocument = clone $objHTML->Document;
$objHTMLDocument->head()
->title("E-Go QR patrol interface")
->cssURL("includes/tables.css");
$body .= "";
// Output: <div style="font-weight:bold;">Hello !!!</div>
#echo $objHTML->div("Hello !!!", NULL, "font-weight:bold;");
$form = new Formr();
// make all fields required
$form->required = '*';
// check if the form was submitted
if($form->submit()) {
// process and validate the POST data
$username = $form->post('uname','Username','slug');
$password = $form->post('passwd','Password','min_length[6]');
$fromDate = $form->post('fromDate','From Date');
$toDate = $form->post('toDate','To Date');
// check if there were any errors
if(!$form->errors()) {
// no errors
// user has entered a valid email address, username, and confirmed their password
echo $form->success_message('Success!');
}
}
// print messages
#echo $form->messages();
#echo $form->form_open();
#echo $form->heading("Title","Batch incident download");
#echo "</br>";
#echo $form->input_text('uname',"Username");
#echo "</br>";
#echo $form->input_password('passwd', "Password");
#echo "</br>";
#echo $form->input_date('fromDate',"From Date", date("Y-m-d", strtotime("-1 months")));
#echo "</br>";
#echo $form->input_date('toDate', "To Date", date("Y-m-d"));
#echo "</br>";
#echo $form->input_submit();
#echo $form->form_close();
$body .= $form->messages();
$body .= $form->form_open();
$body .= $form->heading("Title","Batch incident download");
$body .= "</br>";
$body .= $form->input_text('uname',"Username");
$body .= "</br>";
$body .= $form->input_password('passwd', "Password");
$body .= "</br>";
$body .= $form->input_date('fromDate',"From Date", date("Y-m-d", strtotime("-1 months")));
$body .= "</br>";
$body .= $form->input_date('toDate', "To Date", date("Y-m-d"));
$body .= "</br>";
$body .= $form->input_submit();
$body .= $form->form_close();
//API Url
$url = 'https://ws.followmystaff.com/api/v3/getToken';
//Initiate cURL.
$ch = curl_init($url);
//The JSON data.
$jsonData = array(
'username' => $username,
'password' => $password
);
//Encode the array into JSON.
$jsonDataEncoded = json_encode($jsonData);
//Tell cURL that we want to send a POST request.
curl_setopt($ch, CURLOPT_POST, 1);
//Attach our encoded JSON string to the POST fields.
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded);
//Set the content type to application/json
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//Execute the request
$result = curl_exec($ch);
$resultArray = json_decode($result);
$apiToken = $resultArray->apiToken;
#echo "<p>results: ".$apiToken."</p>";
#$fromDate ="2019-08-01 00:00:00";
#$fromDate ="2019-08-01 00:00:00";
$toDate = strtotime($toDate);;
$fromDate = strtotime($fromDate);
//API Url
$url2 = 'https://ws.followmystaff.com/api/v3/transactions?limit=5&fromDate='.$fromDate.'&toDate='.$toDate;
#$url2 = 'https://ws.followmystaff.com/api/v3/transactions?limit=5&fromDate=1565020800&toDate=1567699200';
#var_dump($url2);
$ch2 = curl_init($url2);
#curl_setopt($ch2, CURLOPT_POST, 1);
curl_setopt ( $ch2, CURLOPT_HTTPHEADER, array ('x-access-token:'.$apiToken) );
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true);
$result2 = curl_exec($ch2);
#var_dump($result2);
$jsonFileName = 'incidents/example.json';
$jfp = fopen($jsonFileName, 'w');
fwrite($jfp, $result2);
fclose($jfp);
echo "<br><br>===========================<br><br><br>\n";
$jsonDecoded = json_decode($result2, true);
//Give our CSV file a name.
$csvFileName = 'incidents/example.csv';
//Open file pointer.
$fp = fopen($csvFileName, 'w');
$fullTable = array();
//Loop through the associative array.
foreach($jsonDecoded["rows"] as $row){
//Write the row to the CSV file.
$rowinfo = array_merge($row["Guard"] , $row["Branch"] , $row["PatrolPoint"]);
# var_dump($rowinfo);
fputcsv($fp, $rowinfo );
array_push($fullTable, $rowinfo);
#var_dump($fullTable);
}
//Finally, close the file pointer.
fclose($fp);
if($apiToken==Null){
echo "Auth Failed";
}
else{
$temp = file_get_contents($csvFileName);
$temp = iconv('UTF-8','cp950',$temp);
file_put_contents($csvFileName,$temp);
#var_dump($fullTable[0]);
$htmlTable = new HtmlTable();
echo $htmlTable->genTable($fullTable);
?>
<a href="http://www.fushuco.com/<?php echo $jsonFileName?>" target="_new">download JSON file</a>
</br>===============================</br>
<a href="http://www.fushuco.com/<?php echo $csvFileName?>">download csv file</a><br><br>
<?php
}
$objHTMLDocument->body()
->content($body);
$objHTMLDocument->display();
?>