function downloadJson($url, $file) { $browser = new Buzz\Browser(); // send request $response = $browser->get($url); $content = $response->getContent(); $filename = __DIR__ . '/fixtures/' . $file; file_put_contents($filename, $content); } // Location $query = new LocationQuery(array('from' => 'Zürich', 'to' => 'Bern')); download($query, 'location.xml'); // Connection $from = new Station('008503000'); $to = new Station('008503504'); $query = new ConnectionQuery($from, $to, array(), '2012-02-13T23:55:00+01:00'); download($query, 'connection.xml'); // Station Board $station = new Station('008591052'); // Zürich, Bäckeranlage $query = new StationBoardQuery($station, \DateTime::createFromFormat(\DateTime::ISO8601, '2012-02-13T23:55:00+01:00', new \DateTimeZone('Europe/Zurich'))); $query->maxJourneys = 3; download($query, 'stationboard.xml'); // Close to Kehrsiten-Bürgenstock $nearBy = new NearbyQuery('47.002347', '8.379934', 2); $url = Transport\API::URL_QUERY . '?' . http_build_query($nearBy->toArray()); downloadJson($url, 'location.json'); // Nyon, rte de l'Etraz $nearBy = new NearbyQuery('46.388653', '6.238729', 1); $url = Transport\API::URL_QUERY . '?' . http_build_query($nearBy->toArray()); downloadJson($url, 'location-nyon.json');
/** * @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); $content = str_replace("\\'", "'", $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; }
/** * @return array */ public function findNearbyLocations(NearbyQuery $query) { $url = self::URL_QUERY . '?' . http_build_query($query->toArray()); // send request $response = $this->browser->get($url); // check for server error if ($response->isServerError()) { throw new \Exception('Server error from fahrplan.sbb.ch: ' . $response->getStatusCode() . ' ' . $response->getReasonPhrase()); } // fix broken JSON $content = $response->getContent(); $content = preg_replace('/(\\w+) ?:/i', '"\\1":', $content); $content = str_replace("\\'", "'", $content); // parse result $result = json_decode($content); // check for JSON error if ($result === null) { throw new \Exception('Invalid JSON from fahrplan.sbb.ch: ' . $content); } $locations = []; 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; }