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
- 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
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;
- 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();
}
- 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;
}