/
API.php
190 lines (152 loc) · 5.35 KB
/
API.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<?php
namespace Transport;
use Buzz\Browser;
use Transport\Entity\Location\Location;
use Transport\Entity\Query;
use Transport\Entity\Location\LocationQuery;
use Transport\Entity\Location\NearbyQuery;
use Transport\Entity\Schedule\ConnectionQuery;
use Transport\Entity\Schedule\ConnectionPageQuery;
use Transport\Entity\Schedule\StationBoardQuery;
class API
{
const URL = 'http://fahrplan.sbb.ch/bin/extxml.exe/';
const URL_QUERY = 'http://fahrplan.sbb.ch/bin/query.exe/dny';
const SBB_PROD = 'iPhone3.1';
const SBB_VERSION = '2.3';
const SBB_ACCESS_ID = 'MJXZ841ZfsmqqmSymWhBPy5dMNoqoGsHInHbWJQ5PTUZOJ1rLTkn8vVZOZDFfSe';
const SEARCH_MODE_NORMAL = 'N';
const SEARCH_MODE_ECONOMIC = 'P';
/**
* @var Buzz\Browser
*/
protected $browser;
/**
* @var string
*/
protected $lang;
public function __construct(Browser $browser = null, $lang = 'EN')
{
$this->browser = $browser ?: new Browser();
$this->lang = $lang;
}
/**
* @return Buzz\Message\Response
*/
public function sendQuery(Query $query, $url = self::URL)
{
$headers = array();
$headers[] = 'User-Agent: SBBMobile/4.2 CFNetwork/485.13.9 Darwin/11.0.0';
$headers[] = 'Accept: application/xml';
$headers[] = 'Content-Type: application/xml';
return $this->browser->post($url, $headers, $query->toXml());
}
/**
* @return array
*/
public function findConnections(ConnectionQuery $query)
{
// send request
$response = $this->sendQuery($query);
// parse result
$result = simplexml_load_string($response->getContent());
// load pages
for ($i = 0; $i < abs($query->page); $i++) {
// load next page
$pageQuery = new ConnectionPageQuery($query, (string) $result->ConRes->ConResCtxt);
$response = $this->sendQuery($pageQuery);
$result = simplexml_load_string($response->getContent());
}
$connections = array();
if ($result->ConRes->ConnectionList->Connection) {
foreach ($result->ConRes->ConnectionList->Connection as $connection) {
$connections[] = Entity\Schedule\Connection::createFromXml($connection, null);
}
}
return $connections;
}
/**
* @return array
*/
public function findLocations(LocationQuery $query)
{
// send request
$response = $this->sendQuery($query);
// parse result
$result = simplexml_load_string($response->getContent());
$locations = array();
$viaCount = 0;
foreach ($result->LocValRes as $part) {
$id = (string) $part['id'];
// A "via" can occur 0-5 times
if ($id == "via") {
$id = $id.(++$viaCount);
}
$locations[$id] = array();
foreach ($part->children() as $location) {
$location = Entity\LocationFactory::createFromXml($location);
if ($location) {
$locations[$id][] = $location;
}
}
}
if (count($locations) > 1) {
return $locations;
}
return reset($locations);
}
/**
* @return array
*/
public function findNearbyLocations(NearbyQuery $query)
{
$url = self::URL_QUERY . '?' . http_build_query($query->toArray());
// send request
$response = $this->browser->get($url);
// fix broken JSON
$content = $response->getContent();
$content = preg_replace('/(\w+) ?:/i', '"\1":', $content);
// parse result
$result = json_decode($content);
$locations = array();
foreach ($result->stops as $stop) {
$location = Entity\LocationFactory::createFromJson($stop);
if ($location) {
$location->distance = $location->coordinate->getDistanceTo($query->lat, $query->lon);
$locations[] = $location;
}
}
return $locations;
}
/**
* @param Entity\Station $station
* @param string $boardType
* @param int $maxJourneys
* @param string $dateTime
* @param array $transportationTypes
*/
public function getStationBoard(StationBoardQuery $query)
{
// send request
$response = $this->sendQuery($query);
// parse result
$result = simplexml_load_string($response->getContent());
// since the stationboard always lists all connections starting from now we just use the date
// and wrap it accordingly if time goes over midnight
$journeys = array();
// subtract one minute because SBB also returns results for one minute in the past
$prevTime = time() - 60;
$date = $query->date;
if ($result->STBRes->JourneyList->STBJourney) {
foreach ($result->STBRes->JourneyList->STBJourney as $journey) {
$curTime = strtotime((string) $journey->MainStop->BasicStop->Dep->Time);
if ($prevTime > $curTime) { // we passed midnight
$date->add(new \DateInterval('P1D'));
}
$journeys[] = Entity\Schedule\StationBoardJourney::createFromXml($journey, $date, null);
$prevTime = $curTime;
}
}
return $journeys;
}
}