Created
April 4, 2016 14:13
-
-
Save mahadirz/b714f9ce34e39a8525a1d121775d8c65 to your computer and use it in GitHub Desktop.
River Water level and flood data in JSON example: https://apps.madet.my/banjir/river_level.php?state=perak
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Created by PhpStorm. | |
* User: Mahadir | |
* Date: 2/6/2016 | |
* Time: 10:04 PM | |
*/ | |
date_default_timezone_set("Asia/Kuala_Lumpur"); | |
set_time_limit ( 300); | |
error_reporting(E_ALL ^ E_NOTICE); | |
try{ | |
$time_start = microtime(true); | |
$subject = file_get_contents('http://publicinfobanjir.water.gov.my/main-page.cfm?switchs=1'); | |
$time_end = microtime(true); | |
$execution_time = ($time_end - $time_start); | |
$expr = '<font.+?>Makluman:<\/font>[\s\S]+?<font.+?>([\s\S]+?)<\/font>[\s\S]+?Masa:[\s\S]+?<font.+?>([\s\S]+?)<\/font>'; | |
$result = preg_match_all("/$expr/i",$subject,$matches); | |
if($result){ | |
$warning_hujan = $matches[1][1]; | |
$time_hujan = $matches[2][1]; | |
$warning_banjir = $matches[1][0]; | |
$time_banjir = $matches[2][0]; | |
header('Content-Type: application/json'); | |
$data = array( | |
array('warning'=>$warning_hujan,'time'=>$time_hujan,'type'=>'rain'), | |
array('warning'=>$warning_banjir,'time'=>$time_banjir,'type'=>'flood'), | |
array('execution_time_seconds'=>$execution_time) | |
); | |
echo json_encode(array('data'=>$data)); | |
} | |
} | |
catch(Exception $e){ | |
$e->getTrace(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Created by PhpStorm. | |
* User: Mahadir | |
* Date: 2/6/2016 | |
* Time: 10:04 PM | |
*/ | |
date_default_timezone_set("Asia/Kuala_Lumpur"); | |
error_reporting(E_ALL ^ E_NOTICE); | |
$state_param = $_GET['state']; | |
header('Content-Type: application/json'); | |
echo json_encode(array("river"=>parse_river_level($state_param))); | |
function parse_river_level($state_param) | |
{ | |
$link = 'http://infobanjir.water.gov.my/waterlevel_page.cfm?'; | |
$state = array( | |
'perlis' => array('state=PLS'), | |
'kedah' => array('state=KDH','state=MAD'), | |
'penang' => array('state=PNG'), | |
'perak' => array('state=PRK'), | |
'selangor' => array('state=SEL'), | |
'kuala_lumpur' => array('state=WLH'), | |
'negeri_sembilan' => array('state=NSN'), | |
'melaka' => array('state=MLK'), | |
'johor' => array('state=JHR'), | |
'pahang' => array('state=PHG'), | |
'terengganu' => array('state=TRG'), | |
'kelantan' => array('state=KEL'), | |
'sabah' => array('state=SAB'), | |
'sarawak' => array('state=SRK'), | |
); | |
if(!array_key_exists($state_param,$state)){ | |
return array('error'=>array('msg'=>'Not Found!')); | |
} | |
$result_all = array(); | |
foreach($state[$state_param] as $param){ | |
$data = file_get_contents($link.$param); | |
//parse row | |
$preg_match = preg_match_all('/<tr height="30">([\s\S]+?)<\/tr>/i',$data,$matches_tr); | |
if($preg_match){ | |
//foreach row | |
foreach($matches_tr[1] as $tr){ | |
$result = array(); | |
//parse column | |
$preg_match = preg_match_all('/<td.*?>([\s\S]+?)<\/td>/i',$tr,$matches_td); | |
if($preg_match){ | |
$tds = $matches_td[1]; | |
//array 1 is station name | |
$preg_match = preg_match_all('/<font[\s\S]*?<font.*?>([\s\S]+?)<\/font>/i',$tds[1],$matches_station); | |
$station_name = $matches_station[1][0]; | |
//array 4 is last update time | |
$preg_match = preg_match_all('/<font.*?>[\s\S]+?<strong>([\s\S]+?)<\/strong>[\s\S]+?<\/font>/i',$tds[4],$matches_last_update_time); | |
$last_update_time = trim($matches_last_update_time[1][0]); | |
//array 5 is river level | |
$preg_match = preg_match_all('/<font[\s\S]*?<font.*?>([\s\S]+?)<\/font>/i',$tds[5],$matches_river_level); | |
$river_level = trim($matches_river_level[1][0]); | |
//array 6 is normal level | |
$preg_match = preg_match_all('/<font[\s\S]+?>(<strong>)?([\s\S]+?)(<\/strong>)?<\/font>/i',$tds[6],$matches_normal_level); | |
$normal_level = trim($matches_normal_level[2][0]); | |
//array 7 is alert level | |
$preg_match = preg_match_all('/<font[\s\S]+?>(<strong>)?([\s\S]+?)(<\/strong>)?<\/font>/i',$tds[7],$matches_alert_level); | |
$alert_level = trim($matches_alert_level[2][0]); | |
//array 8 is alert level | |
$preg_match = preg_match_all('/<font[\s\S]+?>(<strong>)?([\s\S]+?)(<\/strong>)?<\/font>/i',$tds[8],$matches_warning_level); | |
$warning_level = trim($matches_warning_level[2][0]); | |
//array 8 is alert level | |
$preg_match = preg_match_all('/<font[\s\S]+?>(<strong>)?([\s\S]+?)(<\/strong>)?<\/font>/i',$tds[9],$matches_danger_level); | |
$danger_level = trim($matches_danger_level[2][0]); | |
$result = array( | |
'station_name' => $station_name, | |
'last_update_time' => $last_update_time, | |
'river_level' => $river_level, | |
'normal_level' => $normal_level, | |
'alert_level' => $alert_level, | |
'warning_level' => $warning_level, | |
'danger_level' => $danger_level, | |
); | |
} | |
$result_all[] = $result; | |
} | |
} | |
} | |
return $result_all; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment