PHP code example of sharpapi / laravel-airports-database-flight-duration-calculator

1. Go to this page and download the library: Download sharpapi/laravel-airports-database-flight-duration-calculator 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/ */

    

sharpapi / laravel-airports-database-flight-duration-calculator example snippets




namespace App\Http\Controllers;

use GuzzleHttp\Exception\GuzzleException;
use SharpAPI\AirportsDatabaseFlightDurationCalculator\AirportsDatabaseFlightDurationCalculatorService;

class AirportsController extends Controller
{
    protected AirportsDatabaseFlightDurationCalculatorService $airportsService;

    public function __construct(AirportsDatabaseFlightDurationCalculatorService $airportsService)
    {
        $this->airportsService = $airportsService;
    }

    /**
     * @throws GuzzleException
     */
    public function searchAirports(string $query)
    {
        $results = $this->airportsService->searchAirports($query);
        
        return response()->json($results);
    }

    /**
     * @throws GuzzleException
     */
    public function getAirportDetails(string $iataCode)
    {
        $airport = $this->airportsService->getAirportByIataCode($iataCode);
        
        return response()->json($airport);
    }

    /**
     * @throws GuzzleException
     */
    public function calculateFlightDuration(string $fromIataCode, string $toIataCode)
    {
        $duration = $this->airportsService->calculateFlightDuration($fromIataCode, $toIataCode);
        
        return response()->json($duration);
    }

    /**
     * @throws GuzzleException
     */
    public function getAirportsByCountry(string $countryCode)
    {
        $airports = $this->airportsService->getAirportsByCountry($countryCode);
        
        return response()->json($airports);
    }
}

use GuzzleHttp\Exception\ClientException;

try {
    $airports = $this->airportsService->searchAirports('London');
} catch (ClientException $e) {
    echo $e->getMessage();
}
bash
php artisan vendor:publish --tag=sharpapi-airports-database-flight-duration-calculator
bash
SHARP_API_KEY=your_api_key_here
SHARP_API_BASE_URL=https://sharpapi.com/api/v1