Skip to content
This repository has been archived by the owner on Aug 23, 2023. It is now read-only.

rmhdev/cercanias

Repository files navigation

Cercanias

Retrieve Renfe's Cercanías[1] information easily.

[1]: Spain's commuter trains service.

Build Status

Requirements

The Cercanias library has the following requirements:

  • PHP 5.3+

Depending on the chosen HttpAdapter, you may need:

  • php5-curl
  • kriswallsmith/Buzz

Installation

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';

How to use it

  1. Choose a HttpAdapter
  2. Choose a Provider
  3. Create a Cercanias object and call getRoute or getTimetable.

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.

Basic classes

HttpAdapter

Responsible for making the HTTP connection. Available adapters:

  • CurlHttpAdapter: cURL
  • BuzzHttpAdapter: Kris Wallsmith's Buzz HTTP client.

Provider

Indicates where is the information taken. Available providers:

Results

  • Route: Information about a specific route.
  • Timetable: Information about trips between stations for a given day.

Other classes

  • Station: information about a station from a Route.
  • Trip: information about a trip from a Timetable.
  • Train: information about departure and arrival time for a train in a Trip.

Changelog

  • 0.0.1 (June 21, 2014): initial release.
  • 0.0.2 (June 22, 2014): added BuzzHttpAdapter.
  • 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): add Cercanias 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.

Copyright and license

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.

Author

My name is Rober Martín H (@rmhdev). I'm a developer from Donostia / San Sebastián.