PHP code example of iaewing / laravel-opensky

1. Go to this page and download the library: Download iaewing/laravel-opensky 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/ */

    

iaewing / laravel-opensky example snippets


use OpenSky\Laravel\Facades\OpenSky;

// Get all current state vectors
$states = OpenSky::getAllStateVectors();

// Get state vectors in a specific area (bounding box)
$states = OpenSky::getAllStateVectors(
    lamin: 45.8389,  // Switzerland
    lomin: 5.9962,
    lamax: 47.8229,
    lomax: 10.5226
);

// Get flights in a time interval (max 1 hour)
$flights = OpenSky::getFlightsInTimeInterval(
    begin: now()->subHour()->timestamp,
    end: now()->timestamp
);

use OpenSky\Laravel\Client\OpenSkyClient;

class FlightController extends Controller
{
    public function __construct(private OpenSkyClient $openSky)
    {
    }

    public function index()
    {
        $states = $this->openSky->getAllStateVectors();
        
        return view('flights.index', compact('states'));
    }
}

// Get all state vectors (rate limited for anonymous users)
$states = OpenSky::getAllStateVectors(?int $time, ?array $icao24, ?float $lamin, ?float $lomin, ?float $lamax, ?float $lomax, ?int $extended);

// Get your own state vectors (

// Get flights in time interval (max 1 hour)
$flights = OpenSky::getFlightsInTimeInterval(int $begin, int $end);

// Get flights by aircraft (max 30 days)
$flights = OpenSky::getFlightsByAircraft(string $icao24, int $begin, int $end);

// Get arrivals by airport (max 7 days)
$flights = OpenSky::getArrivalsByAirport(string $airport, int $begin, int $end);

// Get departures by airport (max 7 days)
$flights = OpenSky::getDeparturesByAirport(string $airport, int $begin, int $end);

// Get track by aircraft
$track = OpenSky::getTrackByAircraft(string $icao24, int $time = 0);

$states = OpenSky::getAllStateVectors();

echo $states->time; // Unix timestamp
foreach ($states->states as $state) {
    echo $state->icao24;        // Aircraft identifier
    echo $state->callsign;      // Flight callsign
    echo $state->originCountry; // Country of origin
    echo $state->latitude;      // Current latitude
    echo $state->longitude;     // Current longitude
    echo $state->baroAltitude;  // Barometric altitude
    echo $state->velocity;      // Ground speed
    echo $state->onGround;      // Is aircraft on ground
    // ... and more properties
}

$flights = OpenSky::getFlightsInTimeInterval($begin, $end);

foreach ($flights->flights as $flight) {
    echo $flight->icao24;
    echo $flight->callsign;
    echo $flight->estDepartureAirport;
    echo $flight->estArrivalAirport;
    echo $flight->firstSeen;
    echo $flight->lastSeen;
}

$track = OpenSky::getTrackByAircraft('3c4b26');

echo $track->icao24;
echo $track->callsign;
echo $track->startTime;
echo $track->endTime;

foreach ($track->path as $waypoint) {
    echo $waypoint->time;
    echo $waypoint->latitude;
    echo $waypoint->longitude;
    echo $waypoint->baroAltitude;
    echo $waypoint->trueTrack;
    echo $waypoint->onGround;
}

// Get all aircraft currently over Germany
$states = OpenSky::getAllStateVectors(
    lamin: 47.3024,
    lomin: 5.8662,
    lamax: 55.0581,
    lomax: 15.0419
);

foreach ($states->states as $aircraft) {
    if (!$aircraft->onGround && $aircraft->velocity > 100) {
        echo "Flight {$aircraft->callsign} at {$aircraft->baroAltitude}m altitude\n";
    }
}

// Get all departures from Frankfurt Airport in the last hour
$flights = OpenSky::getDeparturesByAirport(
    'EDDF',
    now()->subHour()->timestamp,
    now()->timestamp
);

$flightCount = $flights->flights->count();
echo "Frankfurt had {$flightCount} departures in the last hour\n";

// Track a specific aircraft's route
$track = OpenSky::getTrackByAircraft('3c4b26', now()->subHour()->timestamp);

$route = $track->path->map(function ($waypoint) {
    return [
        'lat' => $waypoint->latitude,
        'lng' => $waypoint->longitude,
        'alt' => $waypoint->baroAltitude,
        'time' => date('H:i:s', $waypoint->time)
    ];
});

// Use $route for map visualization

return [
    // API endpoint
    'base_url' => env('OPENSKY_BASE_URL', 'https://opensky-network.org/api'),
    
    // Authentication (set in .env file)
    'username' => env('OPENSKY_USERNAME'),
    'password' => env('OPENSKY_PASSWORD'),
    'client_id' => env('OPENSKY_CLIENT_ID'),
    'client_secret' => env('OPENSKY_CLIENT_SECRET'),
    'oauth_token_url' => env('OPENSKY_OAUTH_TOKEN_URL', 'https://auth.opensky-network.org/auth/realms/opensky-network/protocol/openid-connect/token'),
    
    // Request timeout
    'timeout' => env('OPENSKY_TIMEOUT', 30),
    
    // Rate limiting
    'rate_limit' => [
        'enabled' => env('OPENSKY_RATE_LIMIT_ENABLED', true),
        'anonymous_per_day' => 400,
        'authenticated_per_day' => 4000,
        'active_feeder_per_day' => 8000,
    ],
    
    // Response caching
    'cache' => [
        'enabled' => env('OPENSKY_CACHE_ENABLED', true),
        'ttl' => env('OPENSKY_CACHE_TTL', 60), // seconds
        'store' => env('OPENSKY_CACHE_STORE', 'default'),
        'prefix' => 'opensky:',
    ],
];

use OpenSky\Laravel\Exceptions\OpenSkyException;

try {
    $states = OpenSky::getAllStateVectors();
} catch (OpenSkyException $e) {
    Log::error('OpenSky API error: ' . $e->getMessage());
}
bash
php artisan vendor:publish --tag=opensky-config