Statens-Vegvesen-PHP

Fetches Statens Vegvesen API data with PHP and renders the data with Twig. Styling with Bootstrap 5.

MIT License

Stars
0

Display vehicle information from Statens Vegvesen

Description

PHP port of https://github.com/w3bdesign/Statens-Vegvesen

Fetching vehicle information from the REST API on https://autosys-kjoretoy-api.atlas.vegvesen.no/api-ui/index-enkeltoppslag.html and displaying it.

It requires an API key that has to be set inside .env (rename .env.example to .env) that you can get from https://www.vegvesen.no/om+statens+vegvesen/om+organisasjonen/apne-data/api-for-tekniske-kjoretoyopplysninger

Features

  • PHP with OOP
  • Separation of HTML and PHP code
  • Composer with class autoloader
  • Error handling and type annotations
  • Twig for rendering HTML
  • Bootstrap 5
  • Responsive design
  • Input validation with HTML5
  • PHPDoc comments

Instructions

To implement this inside your code, you'll need to do the following:

  1. Run composer install

  2. Import the autoloader with require_once "vendor/autoload.php";

  3. Include the class in your project by adding the use statement at the top of your file:


use Vehicle\VehicleDataFetcher;
use Vehicle\VehicleDataRender;

  1. Create a new instance of the VehicleDataFetcher class:
$hasError = false;

if ($_SERVER["REQUEST_METHOD"] === "POST") {
 try {
 	$vehicleDataFetcher = new VehicleDataFetcher();
    // This should be fetched from an input text value with $_POST
 	$regNummer = "AX58167;
 	$vehicleData = $vehicleDataFetcher->getVehicleData($regNummer);
 } catch (Exception $e) {
 	$hasError = true;
 }
}

4.Render the data inside your code:

if (isset($vehicleData)) {
$vehicleDataRender = new VehicleDataRender($vehicleData);
echo $vehicleDataRender->render();
}
  1. Handle the returned data or error message:
if ($hasError) {
echo "<div class='container mt-5 text-center'>
<div class='alert alert-danger' role='alert'>
" . $e->getMessage() . "
</div>
</div>";
return;
}