Skip to content

Commit

Permalink
Updated API to use JAKIM JSON data. Now also return Hijri date
Browse files Browse the repository at this point in the history
  • Loading branch information
afzafri committed Jan 6, 2020
1 parent dbc0fd4 commit 9a9f97c
Showing 1 changed file with 83 additions and 26 deletions.
109 changes: 83 additions & 26 deletions api.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,42 +36,45 @@
{
$kodzon = $_GET['zon']; # store get parameter in variable

$xmlurl = "https://www.e-solat.gov.my/index.php?r=esolatApi/xmlfeed&zon=".$kodzon; # url of JAKIM eSolat XML data
$jsonurl = "https://www.e-solat.gov.my/index.php?r=esolatApi/TakwimSolat&period=today&zone=".$kodzon; # url of JAKIM eSolat JSON data

# fetch xml file (from JAKIM website)
# fetch JSON data (from JAKIM website)
$ch = curl_init(); # initialize curl object
curl_setopt($ch, CURLOPT_URL, $xmlurl);
curl_setopt($ch, CURLOPT_URL, $jsonurl);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$fetchxml = curl_exec($ch); # execute curl, fetch webpage content (xml data)
$fetchjson = curl_exec($ch); # execute curl, fetch webpage content (JSON data)

# parse xml data into object
$data = simplexml_load_string($fetchxml); # parse xml data into object
# parse json data into array
$data = json_decode($fetchjson, true);

curl_close($ch); # close curl

# access xml data, get name of zone, trim object
$namazon = trim($data->channel[0]->link);
$tarikhmasa = trim($data->channel[0]->children('dc',true)->date); # use children() to access tag with colon (:)
# get name of zone
$zones = getZones();
$namazon = $zones[$kodzon];
# access data
$tarikhmasa = $data['serverTime'];
$bearing = $data['bearing'];
$prayerTime = $data['prayerTime'][0];

# create associative array to store waktu solat
$arrwaktu = array();

# iterate through the "item" tag in the xml, access, and store into array
foreach($data->channel[0]->item as $item)
{
# access data and trims object, to store as string
$solat = "waktu_" . strtolower(trim($item->title)); # append "waktu_" to the lowercase string. ex: "waktu_subuh"
$waktu = trim($item->description);

# store into associative array
$arrwaktu[$solat] = convertTime($waktu);
}

# add kod_zon, nama_zon, and tarikh_masa to the array
$arrwaktu["kod_zon"] = $kodzon;
$arrwaktu["nama_zon"] = $namazon;
$arrwaktu["tarikh_masa"] = $tarikhmasa;
$arrwaktu = array(
"waktu_imsak" => convertTime($prayerTime['imsak']),
"waktu_subuh" => convertTime($prayerTime['fajr']),
"waktu_syuruk" => convertTime($prayerTime['syuruk']),
"waktu_zohor" => convertTime($prayerTime['dhuhr']),
"waktu_asar" => convertTime($prayerTime['asr']),
"waktu_maghrib" => convertTime($prayerTime['maghrib']),
"waktu_isyak" => convertTime($prayerTime['isha']),
"tarikh_gregory" => strftime("%d/%m/%Y", malayStrtotime($prayerTime['date'])),
"tarikh_hijrah" => jakimHijri($prayerTime['hijri']),
"hari" => translateDay($prayerTime['day']),
"kod_zon" => $kodzon,
"nama_zon" => $namazon,
"tarikh_server" => $tarikhmasa,
"bearing" => $bearing,
);

# display data in json format
echo json_encode($arrwaktu);
Expand Down Expand Up @@ -109,4 +112,58 @@ function convertTime($time)
return $newtime;
}

// Function to get all zones list
function getZones()
{
$jsonFile = file_get_contents("./zone.json");
$jsonDat = json_decode($jsonFile, true);
$zoneList = array();

foreach ($jsonDat as $key => $value)
{
$zoneList = array_merge($zoneList, $value);
}

# display data in json format
return $zoneList;
}

// Function to replace malay months to english
function malayStrtotime($date_string) {
return strtotime(strtr(strtolower($date_string), array('jan'=>'jan','feb'=>'feb','mac'=>'march','apr'=>'apr','mei'=>'may','jun'=>'jun','jul'=>'jul','ogos'=>'aug','sep'=>'sep','okt'=>'oct','nov'=>'nov','dis'=>'dec')));
}

// Function to translate day to BM
function translateDay($day) {
$days = array(
"Monday" => "Isnin",
"Tuesday" => "Selasa",
"Wednesday" => "Rabu",
"Thursday" => "Khamis",
"Friday" => "Jumaat",
"Saturday" => "Sabtu",
"Sunday" => "Ahad",
);

return $days[$day];
}

// Function to convert hijri date
function jakimHijri($hijri)
{
$hijri = explode("-", $hijri);

$y = $hijri[0];
$m = $hijri[1];
$d = $hijri[2];

$hijriMonth = array(
"Muharram", "Safar", "Rabiulawal", "Rabiulakhir",
"Jamadilawal", "Jamadilakhir", "Rejab", "Syaaban",
"Ramadhan", "Syawal", "Zulkaedah", "Zulhijjah"
);

return $d . " " . $hijriMonth[$m-1] . " " . $y;
}

?>

3 comments on commit 9a9f97c

@zulshafiq11
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Untuk convert Gregorian date to Hijri date ada ke bang?

@afzafri
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Untuk convert Gregorian date to Hijri date ada ke bang?

Boleh pakai ni: https://gist.github.com/faizshukri/7804628

@zulshafiq11
Copy link

@zulshafiq11 zulshafiq11 commented on 9a9f97c Apr 11, 2023 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.