瑞士星曆(Swiss Ephemeris)是由 Astrodienst 開發的高精度星曆,其函式庫在各家占星軟體皆能見其蹤影。而 PHP 環境下也有包裝好的擴充套件,就讓我們先踏出第一步,完成開發環境。

環境

  • Ubuntu 14.04
  • Nginx + php-fpm
  • php 5.5.9

安裝

lib(選擇性)

sudo apt-get install -y re2c

Swiss Ephemeris

cd /tmp
wget -nc http://www.astro.com/ftp/swisseph/swe_unix_src_2.00.00.tar.gz
tar xzf swe_unix_src_2.00.00.tar.gz
cd src
make clean
make libswe.a

複製 libswe.a/usr/lib/

sudo cp libswe.a /usr/lib/
sudo chown root:root /usr/lib/libswe.a

複製 sweodef.hswephexp.h/usr/include/

sudo cp sweodef.h swephexp.h /usr/include/
sudo chown root:root /usr/include/sweodef.h
sudo chown root:root /usr/include/swephexp.h

php-sweph

cd /tmp
wget -nc https://php-sweph.googlecode.com/files/php-sweph-1.80.tgz
tar xzf php-sweph-1.80.tgz
cd php-sweph/
phpize
./configure --enable-sweph
make
cd modules

複製 sweph.so20121212 是版號日期,也許各環境不同,須注意。 偵測方法:

ls /usr/lib/php5/ | grep '^[0-9]\+$'

執行:

sudo cp sweph.so /usr/lib/php5/20121212/
sudo chown root:root /usr/lib/php5/20121212/sweph.so

Swiss Ephemeris 資料

cd /tmp
wget -nc http://www.astro.com/ftp/swisseph/ephe/archive_gzip/sweph_18.tar.gz
tar xzf sweph_18.tar.gz
sudo chown root:root *.se1
sudo mkdir /usr/local/share/sweph
sudo mv *.se1 /usr/local/share/sweph

sweph_18.tar.gz 資料範圍為 1800 AD - 2400 AD,不夠可以自己再多下載。

設定 PHP

建立 sweph.ini

cat << EOF >> sweph.ini
extension=sweph.so
EOF

sudo chown root:root sweph.ini
sudo mv sweph.ini /etc/php5/mods-available/

接著連結模組:

cd /etc/php5/cli/conf.d
sudo ln -sf ../../mods-available/sweph.ini sweph.ini
cd /etc/php5/fpm/conf.d
sudo ln -sf ../../mods-available/sweph.ini sweph.ini

重啟 PHP:

sudo service php5-fpm restart

測試安裝結果

php -i | grep sweph

# 輸出:
/etc/php5/cli/conf.d/sweph.ini
sweph
sweph support => enabled

測試程式

官網範例:

<?php

    swe_set_ephe_path('/usr/local/share/sweph');

    list($y, $m, $d, $h, $mi, $s) = sscanf(gmdate("Y m d G i s"), "%d %d %d %d %d %d");
    $jul_ut = swe_julday($y, $m, $d, ($h + $mi / 60 + $s / 3600), SE_GREG_CAL);

    echo '<table>';
    echo "<tr><td colspan=5>Date: $y-$m-$d $h:$mi:$s</td></tr>";
    echo "<tr><td colspan=5>Julian Date: $jul_ut</td></tr>";

    for($i = SE_SUN; $i <= SE_VESTA; $i++)
    {
        if ($i == SE_EARTH) continue;

        echo '<tr>';
        echo '<td>' . swe_get_planet_name($i) . '</td>';

        $xx = swe_calc_ut($jul_ut, $i, SEFLG_SPEED);
        if ($xx['rc'] < 0) { // error calling swe_calc_ut();
            echo "<td colspan=4>" . $xx['serr'] . "</td>";
            continue;
        }
        echo '<td>' . $xx[0] . '</td>';
        echo '<td>' . $xx[1] . '</td>';
        echo '<td>' . $xx[2] . '</td>';
        echo '<td>' . $xx[3] . '</td>';
        echo '</tr>';
    }
    echo '</table>';

輸出:

Date: 2014-5-8 17:31:49
Julian Date: 2456786.2304282
Sun48.046934485701-0.000110302254217571.00933500606980.96720783634284
Moon155.57453686241-4.18630733882720.002680446977875912.055474065008
Mercury62.1614811949171.71002806139651.17726848567541.9273970278723
Venus6.4639200236421-1.61121249526611.03907467237411.1435593559941
Mars189.865313216391.26272894886240.67163852976878-0.14948157913919
Jupiter106.177046619140.309189316145125.70139215816990.16316961506248
Saturn230.171354686772.4843125569998.9003216592223-0.075141882322412
Uranus14.459547319696-0.6496892897870220.8579927757450.049733758674056
Neptune337.31711618569-0.6993594816446530.2934091723520.017091485593901
Pluto283.441678765412.662831689536732.064051415535-0.011429621070316
mean Node207.5036144739700.0025695552899546-0.052975613790986
true Node208.2510635077300.002552747322350.019136034551145
mean Apogee127.28524747603-5.07098760754480.00271062513188560.11200829653211
osc. Apogee125.11716931203-5.22136851656750.0027051150540632-1.786571638351
Chiron346.943213132364.824352942312718.2855164493260.03680008013925
Pholus263.5490401849816.09222711668724.97393649368-0.02727911506115
Ceres200.4407609877912.5414451304471.7209741614103-0.15378420382947
Pallas148.225893200130.548392306097651.88400075810610.19969917610304
Juno36.9631039348-5.84004982053.00476868281820.57585482868684
Vesta197.9250891083111.7782170622511.2816560060747-0.15429845425954

結語

安裝完成只是第一步,接下來的開發應用才是更深遠的坑!