PHP code example of weble / ptv-php

1. Go to this page and download the library: Download weble/ptv-php 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/ */

    

weble / ptv-php example snippets


use PTV\PTV;

$ptv = new PTV('[YOUR-API-KEY]');

$profiles = $ptv->data()->vehicleProfiles()->all();
$route $ptv->routing()->route()->calculate(['lat,lng', 'lat2,lng2']);

use PTV\PTV;

$ptv = new PTV('[YOUR-API-KEY]', 'it');

use PTV\PTV;

$ptv = new PTV('[YOUR-API-KEY]');
$profiles = $ptv->data()->vehicleProfiles()->all();

use PTV\Data\Enums\VehicleType;
use PTV\PTV;

$ptv = new PTV('[YOUR-API-KEY]');

$profiles = $ptv->data()->vehicleModels()->all();
$profiles = $ptv->data()->vehicleModels()->all([
    VehicleType::TRAILER,
    VehicleType::SEMI_TRAILER,
]);

use PTV\Data\Enums\VehicleType;
use PTV\PTV;

$ptv = new PTV('[YOUR-API-KEY]');

$mapInfo = $ptv->data()->mapInformation()->all();

use Money\Currency;
use PTV\Data\Enums\EngineType;
use PTV\Data\Enums\FuelType;
use PTV\PTV;
use PTV\Routing\DTO\MonetaryCostOptions;
use PTV\Routing\DTO\Options;
use PTV\Routing\DTO\Vehicle;
use PTV\Routing\Enums\ResultType;
use PTV\Routing\Enums\TrafficMode;

$ptv = new PTV('[YOUR-API-KEY]');
$route = $ptv
    ->routing()
    ->route()
    ->return([
        ResultType::MONETARY_COSTS,
            ResultType::POLYLINE,
            ResultType::LEGS,
            ResultType::LEGS_POLYLINE,
            ResultType::ROUTE_ID,
            ResultType::TOLL_COSTS,
            ResultType::TOLL_SYSTEMS,
            ResultType::TOLL_SECTIONS,
            ResultType::TOLL_EVENTS,
            ResultType::ALTERNATIVE_ROUTES,
            ResultType::GUIDED_NAVIGATION,
    ])
    ->forVehicle(
         new Vehicle(
            engineType: EngineType::COMBUSTION,
            fuelType: FuelType::DIESEL,
            numberOfAxles: 2,
            totalPermittedWeight: 75000,
         )
    )
    ->withCostOptions(
        new MonetaryCostOptions(
             costPerKilometer: 1.2
        )
    )
    ->withOptions(
        new Options(
            startTime: DateTimeImmutable::createFromFormat('Y-m-d H:i:s', '2025-01-01 09:00:00'),
            trafficMode: TrafficMode::AVERAGE,
            currency: new Currency('EUR'),
        )
    )
    ->calculate([
        "45.5422993,11.5220921",
        "53.5418064,9.9991367"
])

use Money\Currency;
use PTV\Data\Enums\EngineType;
use PTV\Data\Enums\FuelType;
use PTV\PTV;
use PTV\Routing\DTO\MonetaryCostOptions;
use PTV\Routing\DTO\Options;
use PTV\Routing\DTO\Vehicle;
use PTV\Routing\Enums\ResultType;
use PTV\Routing\Enums\TrafficMode;

$ptv = new PTV('[YOUR-API-KEY]');
$route = $ptv
    ->routing()
    ->route()
    ->return([
        ResultType::MONETARY_COSTS,
    ])
    ->withCostOptions(
        new MonetaryCostOptions(
             costPerKilometer: 1.2
        )
    )
    ->withOptions(
        new Options(
            startTime: DateTimeImmutable::createFromFormat('Y-m-d H:i:s', '2025-01-01 09:00:00'),
            trafficMode: TrafficMode::AVERAGE,
            currency: new Currency('EUR'),
        )
    )
    ->recalculate('[your-route-id]');

use Money\Currency;
use PTV\Data\Enums\EngineType;
use PTV\Data\Enums\FuelType;
use PTV\PTV;
use PTV\Routing\DTO\MonetaryCostOptions;
use PTV\Routing\DTO\Options;
use PTV\Routing\DTO\Vehicle;
use PTV\Routing\Enums\ResultType;
use PTV\Routing\Enums\TrafficMode;

$ptv = new PTV('[YOUR-API-KEY]');
$route = $ptv
    ->routing()
    ->route()
    ->get('[your-route-id]');

use PTV\Routing\DTO\Route;
/** @var Route  $route **/

$route->alternativeRoutes;
$route->monetaryCosts->distanceCost;
$route->toll->costs;

// ...