Fetches Statens Vegvesen API data with PHP and renders the data with Twig. Styling with Bootstrap 5.
MIT License
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
To implement this inside your code, you'll need to do the following:
Run composer install
Import the autoloader with require_once "vendor/autoload.php";
Include the class in your project by adding the use
statement at the top of your file:
use Vehicle\VehicleDataFetcher;
use Vehicle\VehicleDataRender;
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();
}
if ($hasError) {
echo "<div class='container mt-5 text-center'>
<div class='alert alert-danger' role='alert'>
" . $e->getMessage() . "
</div>
</div>";
return;
}