Retrieve Renfe's Cercanías[1] information easily.
[1]: Spain's commuter trains service.
The Cercanias
library has the following requirements:
PHP 5.3+
Depending on the chosen HttpAdapter
, you may need:
php5-curl
kriswallsmith/Buzz
The recommended way to install Cercanias
is through Composer.
Create a composer.json
file:
{
"require": {
"rmhdev/cercanias": "0.2.*"
}
}
Then install Composer
:
$ curl -sS https://getcomposer.org/installer | php
$ composer install
After that, the autoloader will be available:
<?php
require 'vendor/autoload.php';
- Choose a
HttpAdapter
- Choose a
Provider
- Create a
Cercanias
object and callgetRoute
orgetTimetable
.
For example, if you want to retrieve all the stations from a route (San Sebastián):
<?php
require 'vendor/autoload.php';
use Cercanias\HttpAdapter\CurlHttpAdapter;
use Cercanias\Provider\HorariosRenfeCom\Provider;
use Cercanias\Cercanias;
$httpAdapter = new CurlHttpAdapter(); // 1. HttpAdapter
$provider = new Provider($httpAdapter); // 2. Provider
$cercanias = new Cercanias($provider); // 3. Cercanias
$route = $cercanias->getRoute(Provider::ROUTE_SAN_SEBASTIAN);
If you want to know all the trips from Brinkola to Irun for tomorrow:
<?php
require 'vendor/autoload.php';
use Cercanias\HttpAdapter\CurlHttpAdapter;
use Cercanias\Provider\HorariosRenfeCom\Provider;
use Cercanias\Provider\TimetableQuery;
use Cercanias\Cercanias;
$query = new TimetableQuery();
$query
->setRoute(Provider::ROUTE_SAN_SEBASTIAN)
->setDeparture("11305") // from Brinkola
->setDestination("11600") // to Irun
->setDate(new \DateTime("tomorrow"));
$httpAdapter = new CurlHttpAdapter(); // 1. HttpAdapter
$provider = new Provider($httpAdapter); // 2. Provider
$cercanias = new Cercanias($provider); // 3. Cercanias
$timetable = $cercanias->getTimetable($query);
View more examples.
Responsible for making the HTTP connection. Available adapters:
CurlHttpAdapter
: cURLBuzzHttpAdapter
: Kris Wallsmith's Buzz HTTP client.
Indicates where is the information taken. Available providers:
Provider\HorariosRenfeCom\Provider
: Cercanias' default web page
Route
: Information about a specific route.Timetable
: Information about trips between stations for a given day.
Station
: information about a station from aRoute
.Trip
: information about a trip from aTimetable
.Train
: information about departure and arrival time for a train in aTrip
.
0.0.1
(June 21, 2014): initial release.0.0.2
(June 22, 2014): addedBuzzHttpAdapter
.0.0.3
(June 24, 2014): simplify timetable queries.0.0.4
(June 29, 2014): fix bugs, improve naming and parsing.0.0.5
(July 01, 2014): improve queries generation.0.0.6
(July 06, 2014): addCercanias
class to simplify usage.0.1.0
(July 06, 2014): add library to Packagist.0.2.0
(July 28, 2014): fix a couple of bugs.
Code and documentation copyright 2014 Rober Martín H. Code released under MIT license. Docs released under Creative Commons CC BY 4.0.
Part of the project is based on William Durand's Geocoder.
My name is Rober Martín H (@rmhdev). I'm a developer from Donostia / San Sebastián.