03 November 2007

Muslim prayer times by web scraping

Here I'll show how to get Muslim pray times by web scraping. Web scraping is one of the methods to implement mashup.


<?php

$curl_handle=curl_init();
curl_setopt($curl_handle,CURLOPT_URL,'http://www.e-solat.gov.my/solat.php?kod=sgr03&lang=ENG');
curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,2);
curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1);
$buffer = curl_exec($curl_handle);
curl_close($curl_handle);

if (empty($buffer)) {
print '<a href="http://www.e-solat.gov.my/">Prayer Time</a>';
} else {
$waktu_s = array('imsak', 'subuh', 'syuruk', 'zohor', 'asar', 'maghrib', 'isyak');
$w = array();
foreach ($waktu_s as $waktu) {
$w[] = "<tr.*?>.*?<td.*?>.*?$waktu.*?<\/td>.*?<td.*?>.*?(?P<
$waktu>\d?\d:\d\d).*?<\/td>.*?<\/tr>";
}
$t = join(".*?", $w);
$regex = "/<table.*?>.*?$t.*?<\/table>/si";
$result = "";
if (preg_match($regex, $buffer, $matches)) {
$result = '<table border="1"><caption>Kuala Lumpur</caption>';
$result .= '<thead><tr>';
foreach ($waktu_s as $waktu) {
$result .= "<th>".ucfirst($waktu)."</th>";
}
$result .= '</tr></thead>';
$result .= '<tbody><tr>';
foreach ($waktu_s as $waktu) {
$result .= "<td>".$matches[$waktu]."</td>";
}
$result .= '</tr></tbody>';
$result .= '</table>';
}
print $result;
}

?>


Example of output as below:
Kuala Lumpur
ImsakSubuhSyurukZohorAsarMaghribIsyak
5:295:396:5813:0016:1918:5820:09


I'll leave to you as exercise to show the date of prayer times, GMT and qiblat direction.
Post a Comment