-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstationScheduleData.php
112 lines (79 loc) · 2.67 KB
/
stationScheduleData.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
100
101
102
103
104
105
106
107
108
109
110
111
112
<?php
$station_code = $_REQUEST["station"];
if (empty($station_code)){
http_response_code(503);
exit;
}
$url = 'http://myrailtime.ktmb.com.my/timetable/?origin='.$station_code;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
if(curl_errno($ch)) {
http_response_code(503);
} else {
$redirectUrl = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
}
curl_close($ch);
// Request new URL
$url = $redirectUrl;
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, 1);
$resp = curl_exec($curl);
$http_response = curl_getinfo($curl, CURLINFO_HTTP_CODE);
$headersize = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
curl_close($curl);
$header = substr($resp, 0, $headersize);
$response = substr($resp,$headersize);
if (preg_match('/X-CSRF-TOKEN-COOKIENAME=([^;]+)/', $header, $matches)) {
$token = $matches[1];
// Create a new DOMDocument object and load the HTML content
$doc = new DOMDocument();
$doc->loadHTML($response);
// Find the input element with name '__RequestVerificationToken'
$input = $doc->getElementsByTagName('input')->item(0);
if ($input->getAttribute('name') == '__RequestVerificationToken') {
$token2 = $input->getAttribute('value');
$regex = '/var allTripDatas = \["(.*?)"\];/';
if (preg_match($regex, $response, $matches)) {
$all = $matches[0];
$re = '/\[".*"\]/';
$str = $all;
if (preg_match($re, $str, $matches)){
$tripdata = $matches[0];
$tripdatadecode = json_decode($tripdata,true);
// loop trip
$responses = array();
foreach ($tripdatadecode as $value){
$url = "https://online.ktmb.com.my/TimeTable/StationTrip";
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$headers = array(
"RequestVerificationToken: ".$token2,
"Cookie: X-CSRF-TOKEN-COOKIENAME=".$token,
"Content-Type: application/json",
);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
$data = '{"TripData":"'.$value.'"}';
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
//for debug only!
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$resp = curl_exec($curl);
curl_close($curl);
$responses[] = $resp;
}
$merged_response = array_merge($responses);
header('Access-Control-Allow-Origin: *');
header('Content-Type: application/json');
echo json_encode($merged_response);
}
}
}
}
?>