DEV Community

Jayesh Patel
Jayesh Patel

Posted on

Swiss Ephemeris PHP: A 1:1 FFI Wrapper for Astronomical Precision in PHP 8.3+

Swiss Ephemeris PHP: A 1:1 FFI Wrapper for Astronomical Precision in PHP 8.3+

If you have ever built an astrology, astronomy, or ephemeris-based feature in PHP, you probably know the common trade-offs: CLI wrappers, slow process spawning, text parsing, or incomplete pure-PHP approximations.

I wanted something better.

So I built Swiss Ephemeris PHP — a framework-agnostic FFI wrapper for the Swiss Ephemeris C library, designed for PHP 8.3+, with prebuilt binaries to make installation easier across platforms.

This package is meant to stay close to the original Swiss Ephemeris API. No unnecessary abstraction. No external CLI process. No text-output parsing. Just direct native function access through PHP FFI.

What is Swiss Ephemeris?

Swiss Ephemeris is one of the most widely used astronomical calculation libraries for planetary positions, sidereal calculations, house systems, eclipses, and more.

It is trusted for high-precision astronomical and astrological work, which is exactly why it is so valuable in software that needs reliable celestial data.

Why I built this package

Most PHP solutions for Swiss Ephemeris fall into one of these categories:

  • CLI wrappers that call a binary and parse output
  • Pure PHP implementations that do not match the native library’s accuracy
  • Compiled PHP extensions that are harder to install and distribute

I wanted a package that gives PHP developers:

  • Direct access to the C library
  • Better performance than CLI-based approaches
  • Easy installation through Composer
  • Support for modern PHP applications
  • Compatibility with frameworks and plain PHP projects

That is the purpose of this wrapper.

What this package provides

Swiss Ephemeris PHP focuses on:

  • Direct FFI calls to the Swiss Ephemeris C library
  • A 1:1 mapping of the underlying API
  • Support for astronomical and astrological calculations
  • Prebuilt binaries for easier setup
  • Framework-agnostic usage
  • Laravel-friendly integration where needed

Why FFI matters

PHP FFI allows PHP to call native libraries directly.

That means you can:

  • Load a compiled shared library
  • Map C types, pointers, and structs
  • Call native functions without writing a PHP extension
  • Keep the development workflow simpler than compiling custom modules

For Swiss Ephemeris, this is a strong fit because it preserves the native behavior of the library while still letting PHP developers use it cleanly.

Example use case

A typical flow looks like this:

$sweph = new SwissEphFFI();

$jd = $sweph->swe_julday(2000, 1, 1, 12.0, SwissEphFFI::SE_GREG_CAL);

$xx = $sweph->getFFI()->new("double[6]");
$serr = $sweph->getFFI()->new("char[256]");

$sweph->swe_calc_ut(
    $jd,
    SwissEphFFI::SE_SUN,
    SwissEphFFI::SEFLG_SPEED,
    $xx,
    $serr
);

echo "Sun longitude: " . $xx[0] . "°";
Enter fullscreen mode Exit fullscreen mode

That style keeps the wrapper close to the original Swiss Ephemeris behavior.

Key highlights

  • 1:1 FFI wrapper for Swiss Ephemeris
  • PHP 8.3+ compatibility
  • Framework-agnostic design
  • Prebuilt binaries for supported platforms
  • Direct access to native astronomical functions
  • Suited for astrology, astronomy, and ephemeris-based applications

Where this fits best

This package is useful for projects like:

  • Astrology engines
  • Planetary position calculators
  • Vedic or sidereal computation tools
  • Astronomy dashboards
  • Calendar and panchanga systems
  • Scientific or educational applications

Installation

composer require jayeshmepani/swiss-ephemeris-ffi
Enter fullscreen mode Exit fullscreen mode

After installation, configure your environment according to your platform and FFI requirements.

Laravel support

Even though the package is framework-agnostic, it can also be integrated into Laravel with service providers or facades for a more convenient developer experience.

Closing thoughts

Swiss Ephemeris PHP is built for developers who want the native Swiss Ephemeris library in PHP without compromising on architecture or forcing a heavy installation process.

If your project depends on accurate celestial calculations, this wrapper is designed to give you a cleaner path into Swiss Ephemeris from PHP.

References

GitHub logo jayeshmepani / Swiss-Ephemeris-PHP

PHP 8.3+ FFI wrapper for the Swiss Ephemeris C library with 1:1 API coverage, prebuilt binaries, and Laravel support.

Swiss Ephemeris PHP FFI

Latest Version on Packagist Total Downloads PHP Version Require License Tests Release Prebuilt Libraries

A strict, 100% precise, exact 1:1 FFI mapping of the Swiss Ephemeris C library for PHP 8.3+.

This wrapper gives you the exact API surface, structs, macro constants, and FFI pointers as defined in the original swephexp.h. Zero tolerance for omissions or high-level abstractions: every single calculation, planet ID, house system, sidereal mode, and eclipse check is strictly preserved for maximum astronomical precision.

🎯 Unique Value Proposition

This is the ONLY PHP implementation that:

  • ✅ Uses PHP FFI (no PHP extension compilation required)
  • ✅ Links to shared library (libswe.* per OS)
  • ✅ Achieves 100% function coverage (all 200+ swe_* functions)
  • ✅ Maintains 1:1 mapping with C library
  • ✅ Preserves exact C library precision (no CLI parsing, no text output)
  • ✅ Works with Laravel, Symfony, or plain PHP

Unlike other PHP packages that wrap the swetest CLI binary, this package calls the C functions directly…

Top comments (0)