-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetJalaliHolidays.php
99 lines (86 loc) · 2.31 KB
/
getJalaliHolidays.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?php
/**
* getJalaliHolidays function written in php
* created by Yasin Jamalzadeh
* 02/24/2024
*
*
* Version: 2.0
*
*
*
* */
//
function convertEntitiesToEnglish($input)
{
$htmlEntities = [
"۰", // ۰
"۱", // ۱
"۲", // ۲
"۳", // ۳
"۴", // ۴
"۵", // ۵
"۶", // ۶
"۷", // ۷
"۸", // ۸
"۹", // ۹
];
$englishNumbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
$convertedString = str_replace($htmlEntities, $englishNumbers, $input);
return $convertedString;
}
$result_array['status'] = false;
$result_array['holidays'] = array();
if (!isset($_REQUEST['year'])) {
$result_array['status'] = false;
$result_array['msg'] = 'The year value is not entered.';
echo json_encode($result_array);
die;
} elseif (!isset($_REQUEST['month'])) {
$result_array['status'] = false;
$result_array['msg'] = 'The month value is not entered.';
echo json_encode($result_array);
die;
}
$url = 'https://www.time.ir/'; //Collecting data from time.ir website
$data = array(
'Year' => $_REQUEST['year'],
'Month' => $_REQUEST['month'],
'Base1' => '0',
'Base2' => '1',
'Base3' => '2',
'Responsive' => 'true'
);
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data)
)
);
$context = stream_context_create($options);
$response = file_get_contents($url, false, $context);
if ($response === false) {
// Handle error
$result_array['status'] = false;
$result_array['msg'] = 'Handle error.';
echo json_encode($result_array);
die;
} else {
$result_array['status'] = true;
//
$pattern = '/>\s+?</';
$replacement = '><';
$response = preg_replace($pattern, $replacement, $response);
//
$pattern = '/<div class=\' holiday\'><div class=\'jalali\' style=\'\'>(.*?)<\/div>/s';
preg_match_all($pattern, $response, $matches);
if (!empty($matches[1])) {
$gottenValue = $matches[1];
$engNum = convertEntitiesToEnglish($gottenValue);
$result_array['holidays'] = $engNum;
}
echo json_encode($result_array);
die;
}
?>