PHP code example of omnitravel / navitia

1. Go to this page and download the library: Download omnitravel/navitia library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

omnitravel / navitia example snippets




mnitravel\Navitia\NavitiaFactory;
use Omnitravel\Testing\RecordedHttpClient;

// Navitia's answer to a search by name, recorded: no token, nothing sent. HttpClient::create() for a real call.
$http = RecordedHttpClient::routes([
    '~^GET https://api\.sncf\.com/v1/coverage/sncf/places\?q=Angers~' => [200, '{"places": [{"id": "stop_area:SNCF:87484006", "embedded_type": "stop_area", "stop_area": {"id": "stop_area:SNCF:87484006", "name": "Angers Saint-Laud", "coord": {"lon": "-0.556583", "lat": "47.464511"}, "timezone": "Europe/Paris"}}]}'],
]);
$sncf = (new NavitiaFactory($http))->create(['token' => 'recorded', 'coverage' => 'sncf', 'base_uri' => 'https://api.sncf.com/v1']);

foreach ($sncf->searchStops('Angers', 1) as $station) {
    echo $station->name, ' ', $station->id, "\n";   // Angers Saint-Laud stop_area:SNCF:87484006
}

use Omnitravel\Model\DepartureQuery;
use Omnitravel\Navitia\NavitiaFactory;

$sncf = (new NavitiaFactory($httpClient))->create([
    'token' => getenv('SNCF_API_TOKEN'),          // by reference: never written in a file
    'coverage' => 'sncf',
    'base_uri' => 'https://api.sncf.com/v1',
    'attribution' => 'Données SNCF',
]);
foreach ($sncf->departures(new DepartureQuery('stop_area:SNCF:87391003', limit: 5)) as $departure) {
    echo $departure->number, ' ', $departure->headsign, ' ', $departure->time()->format('H:i'), ' voie ', $departure->platform, "\n";
}