An API to convert CSV data from Ministry Of Health - Malaysia data into JSON file
- Cases (with state level)
- Deaths (with state level)
- Vaccination
- Clusters
Build with pure PHP script
Please read https://covid-19.samsam123.name.my/api.html for more information.
Requirement :
- PHP 7.0 and above , 7.4 is recommended
- Connected with Internet
- Able to access Github
Steps :
- Copy the 6 of the PHP file to ur webroot directory with PHP enabled.
- Start using it now by reading the instructions here.
Credit : MoH-Malaysia https://www.webslesson.info/2020/10/how-to-convert-csv-to-json-in-php.html
Requirement :
- Redis Installed with no forcing password authentication
- Redis for PHP (Extension) Installed
Add code below to each PHP file before the original PHP code :
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
$max_calls_limit = 30;
$time_period = 30;
$total_user_calls = 0;
if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
$user_ip_address = $_SERVER['HTTP_CLIENT_IP'];
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$user_ip_address = $_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
$user_ip_address = $_SERVER['REMOTE_ADDR'];
}
if (!$redis->exists($user_ip_address)) {
$redis->set($user_ip_address, 1);
$redis->expire($user_ip_address, $time_period);
$total_user_calls = 1;
} else {
$redis->INCR($user_ip_address);
$total_user_calls = $redis->get($user_ip_address);
if ($total_user_calls > $max_calls_limit) {
$json = array("Status"=>"Fail","Message"=>"Rate limit exceeded!","IP Address"=>$user_ip_address,"Total_Calls"=>$total_user_calls,"Period"=>$time_period);
header('Content-type: application/json'); header('Access-Control-Allow-Origin: *');
header('HTTP/1.1 429 Too Many Requests');
echo (json_encode($json));
exit();
}
}
Place these code BEFORE the original PHP code Example :
<?php
[PLACE HERE THE RATE LIMIT CODE!]
$date_request = $_GET["date"];
if (empty($date_request)) {
$json = array("Status"=>"Fail","Message"=>"Did not provide query string - date. Please refer to https://covid-19.samsam123.name.my/api.html for more information.");
header('Content-type: application/json'); header('Access-Control-Allow-Origin: *');
echo (json_encode($json)); ..........
?>
Final :
<?php
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
$max_calls_limit = 30;
$time_period = 30;
$total_user_calls = 0;
if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
$user_ip_address = $_SERVER['HTTP_CLIENT_IP'];
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$user_ip_address = $_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
$user_ip_address = $_SERVER['REMOTE_ADDR'];
}
if (!$redis->exists($user_ip_address)) {
$redis->set($user_ip_address, 1);
$redis->expire($user_ip_address, $time_period);
$total_user_calls = 1;
} else {
$redis->INCR($user_ip_address);
$total_user_calls = $redis->get($user_ip_address);
if ($total_user_calls > $max_calls_limit) {
$json = array("Status"=>"Fail","Message"=>"Rate limit exceeded!","IP Address"=>$user_ip_address,"Total_Calls"=>$total_user_calls,"Period"=>$time_period);
header('Content-type: application/json'); header('Access-Control-Allow-Origin: *');
header('HTTP/1.1 429 Too Many Requests');
echo (json_encode($json));
exit();
}
}
$date_request = $_GET["date"];
if (empty($date_request)) {
$json = array("Status"=>"Fail","Message"=>"Did not provide query string - date. Please refer to https://covid-19.samsam123.name.my/api.html for more information.");
header('Content-type: application/json'); header('Access-Control-Allow-Origin: *');
echo (json_encode($json)); ..........
?>
The rate limit code is same as the rate limit section on API section
Any IP Address that exceed 30 requests in 30 seconds will receive a 429 Too Many Requests
and a JSON string contains IP Address
and Total Call Requests in 30 seconds
.
The rate limit will be auto reset each 30 seconds.
Make a PR if you want to add your project that utilize this API above.