Api Fixed

This commit is contained in:
2026-03-10 07:01:52 +05:45
parent 1839a4d28b
commit 22136607e1
3 changed files with 210 additions and 55 deletions

View File

@@ -120,5 +120,12 @@ function sortPlanetsByLongitude(array $planets): array
function getSegmentFromLongitude(float $longitude): int function getSegmentFromLongitude(float $longitude): int
{ {
$longitude = normalizeLongitude($longitude); $longitude = normalizeLongitude($longitude);
// Each zodiac sign = 30°
// 0-30 → 1
// 30-60 → 2
// ...
// 330-360 → 12
return ((int) floor($longitude / 30)) + 1; return ((int) floor($longitude / 30)) + 1;
} }

128
ephemeris_engine.php Normal file
View File

@@ -0,0 +1,128 @@
<?php
require_once 'astro_functions.php';
const SWISSEPH_API_BASE_URL = 'http://bibgit.com:8081/api.php';
const SWISSEPH_API_TIMEOUT = 20;
function buildSwissephApiUrl(
string $date,
string $time,
float $latitude,
float $longitude,
string $timezone = 'Asia/Kathmandu'
): string {
return SWISSEPH_API_BASE_URL
. '?date=' . urlencode($date)
. '&time=' . urlencode($time)
. '&timezone=' . urlencode($timezone)
. '&latitude=' . urlencode((string)$latitude)
. '&longitude=' . urlencode((string)$longitude);
}
function fetchRemoteSwissephResponse(
string $date,
string $time,
float $latitude,
float $longitude,
string $timezone = 'Asia/Kathmandu'
): array {
$url = buildSwissephApiUrl($date, $time, $latitude, $longitude, $timezone);
if (function_exists('curl_init')) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, SWISSEPH_API_TIMEOUT);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
$response = curl_exec($ch);
$httpCode = (int) curl_getinfo($ch, CURLINFO_HTTP_CODE);
$curlErr = curl_error($ch);
curl_close($ch);
if ($response === false || $httpCode !== 200) {
return [
'success' => false,
'error' => $curlErr ?: ('Remote API returned HTTP ' . $httpCode),
'data' => []
];
}
} else {
$context = stream_context_create([
'http' => [
'timeout' => SWISSEPH_API_TIMEOUT
]
]);
$response = @file_get_contents($url, false, $context);
if ($response === false) {
return [
'success' => false,
'error' => 'Could not connect to remote Swiss Ephemeris API.',
'data' => []
];
}
}
$decoded = json_decode($response, true);
if (!is_array($decoded)) {
return [
'success' => false,
'error' => 'Invalid JSON received from remote API.',
'data' => []
];
}
return [
'success' => true,
'error' => '',
'data' => $decoded
];
}
function getRealPlanetaryData(
string $date,
string $time,
float $latitude,
float $longitude,
string $timezone = 'Asia/Kathmandu'
): array {
$remote = fetchRemoteSwissephResponse($date, $time, $latitude, $longitude, $timezone);
if (!$remote['success']) {
return [
'planets' => [],
'ascendant' => null,
'error' => $remote['error']
];
}
$data = $remote['data'];
$results = [];
if (isset($data['planets']) && is_array($data['planets'])) {
foreach ($data['planets'] as $planet) {
if (!is_array($planet) || !isset($planet['key']) || !isset($planet['longitude'])) {
continue;
}
if (!in_array($planet['key'], PLANET_KEYS, true)) {
continue;
}
$results[] = [
'key' => $planet['key'],
'longitude' => normalizeLongitude((float)$planet['longitude'])
];
}
}
return [
'planets' => $results,
'ascendant' => isset($data['ascendant']) ? (float)$data['ascendant'] : null,
'error' => ''
];
}

130
index.php
View File

@@ -1,9 +1,9 @@
<?php <?php
require_once 'constants.php'; require_once 'constants.php';
require_once 'astro_functions.php'; require_once 'astro_functions.php';
require_once 'ephemeris_engine.php';
$nameMode = NAME_MODE_MODERN; $nameMode = NAME_MODE_VEDIC;
$nameMode=NAME_MODE_VEDIC;
date_default_timezone_set('Asia/Kathmandu'); date_default_timezone_set('Asia/Kathmandu');
$date = $_GET['date'] ?? ''; $date = $_GET['date'] ?? '';
@@ -28,6 +28,13 @@ $utcDateTime = '';
$unixTimestamp = ''; $unixTimestamp = '';
$julianDay = ''; $julianDay = '';
$decimalHour = ''; $decimalHour = '';
$apiError = '';
$samplePlanets = [];
$planetsByHierarchy = [];
$positionsInOrder = [];
$ascendantLongitude = null;
$ascendantData = null;
if (!empty($location) && isset($locationMap[$location])) { if (!empty($location) && isset($locationMap[$location])) {
$latitude = $locationMap[$location]['latitude']; $latitude = $locationMap[$location]['latitude'];
@@ -79,32 +86,34 @@ if (!empty($date) && !empty($time) && !empty($location) && isset($locationMap[$l
$unixTimestamp = $calcData['unix_timestamp']; $unixTimestamp = $calcData['unix_timestamp'];
$julianDay = $calcData['julian_day']; $julianDay = $calcData['julian_day'];
$decimalHour = $calcData['decimal_hour']; $decimalHour = $calcData['decimal_hour'];
}
/* $realPlanetData = getRealPlanetaryData($date, $time, (float)$latitude, (float)$longitude);
SAMPLE PLANET DATA $realPlanetLongitudes = $realPlanetData['planets'];
house = wheel segment number $ascendantLongitude = $realPlanetData['ascendant'];
*/ $apiError = $realPlanetData['error'];
$samplePlanets = [
buildPlanetDataFromLongitude('sun', 5.20, $nameMode), if (!empty($realPlanetLongitudes)) {
buildPlanetDataFromLongitude('moon', 72.13, $nameMode), foreach ($realPlanetLongitudes as $planet) {
buildPlanetDataFromLongitude('mars', 78.35, $nameMode), $samplePlanets[] = buildPlanetDataFromLongitude(
buildPlanetDataFromLongitude('mercury', 32.08, $nameMode), $planet['key'],
buildPlanetDataFromLongitude('venus', 41.30, $nameMode), $planet['longitude'],
buildPlanetDataFromLongitude('jupiter', 129.18, $nameMode), $nameMode
buildPlanetDataFromLongitude('saturn', 22.43, $nameMode), );
buildPlanetDataFromLongitude('rahu', 304.23, $nameMode), }
buildPlanetDataFromLongitude('ketu', 124.23, $nameMode) } elseif (empty($apiError)) {
]; $apiError = 'Could not fetch planetary data from remote API.';
}
if ($ascendantLongitude !== null) {
$ascendantData = buildPlanetDataFromLongitude('sun', $ascendantLongitude, $nameMode);
}
}
$planetsByHierarchy = $samplePlanets; $planetsByHierarchy = $samplePlanets;
$positionsInOrder = sortPlanetsByLongitude($samplePlanets); $positionsInOrder = sortPlanetsByLongitude($samplePlanets);
?> ?>
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en"> <html lang="en">
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
@@ -113,7 +122,6 @@ $positionsInOrder = sortPlanetsByLongitude($samplePlanets);
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet"> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="css.css" rel="stylesheet"> <link href="css.css" rel="stylesheet">
</head> </head>
<body> <body>
<div class="container py-5"> <div class="container py-5">
@@ -183,8 +191,20 @@ $positionsInOrder = sortPlanetsByLongitude($samplePlanets);
</div> </div>
</div> </div>
<?php if ($ascendantLongitude !== null && $ascendantData !== null): ?>
<div class="mt-3">
<p><strong>Ascendant Longitude:</strong> <?= htmlspecialchars((string)round($ascendantLongitude, 6)) ?></p>
<p><strong>Ascendant Sign:</strong> <?= htmlspecialchars($ascendantData['sign']) ?></p>
<p><strong>Ascendant Degree:</strong> <?= htmlspecialchars($ascendantData['degree']) ?></p>
<p><strong>Ascendant Nakshatra:</strong> <?= htmlspecialchars($ascendantData['nakshatra']) ?></p>
</div>
<?php endif; ?>
<?php if (!empty($apiError)): ?>
<div class="alert alert-danger mt-3 mb-0">
<?= htmlspecialchars($apiError) ?>
</div>
<?php endif; ?>
</div> </div>
<?php else: ?> <?php else: ?>
<div class="result-box mt-3"> <div class="result-box mt-3">
@@ -200,13 +220,13 @@ $positionsInOrder = sortPlanetsByLongitude($samplePlanets);
</div> </div>
</div> </div>
</div> </div>
<div class="row g-4 mt-4"> <div class="row g-4 mt-4">
<div class="col-lg-6"> <div class="col-lg-6">
<div class="table-box"> <div class="table-box">
<h5 class="table-title">Table 1: Positions by Planets</h5> <h5 class="table-title">Table 1: Positions by Planets</h5>
<div class="table-responsive"> <div class="table-responsive">
<table class="table table-dark table-bordered align-middle astro-table mb-0"> <table class="table table-dark table-bordered align-middle astro-table mb-0">
<thead> <thead>
<tr> <tr>
<th>Planet</th> <th>Planet</th>
@@ -216,21 +236,22 @@ $positionsInOrder = sortPlanetsByLongitude($samplePlanets);
<th>Nakshatra</th> <th>Nakshatra</th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
<?php if (!empty($planetsByHierarchy)): ?>
<?php foreach ($planetsByHierarchy as $planet): ?> <?php foreach ($planetsByHierarchy as $planet): ?>
<tr>
<td><?= htmlspecialchars($planet['name']) ?></td>
<td><?= htmlspecialchars($planet['house']) ?></td>
<td><?= htmlspecialchars($planet['sign']) ?></td>
<td><?= htmlspecialchars($planet['degree']) ?></td>
<td><?= htmlspecialchars($planet['nakshatra']) ?></td>
</tr>
<?php endforeach; ?>
<?php else: ?>
<tr> <tr>
<td><?= htmlspecialchars($planet['name']) ?></td> <td colspan="5">No planetary data available.</td>
<td><?= htmlspecialchars($planet['house']) ?></td>
<td><?= htmlspecialchars($planet['sign']) ?></td>
<td><?= htmlspecialchars($planet['degree']) ?></td>
<td><?= htmlspecialchars($planet['nakshatra']) ?></td>
</tr> </tr>
<?php endif; ?>
<?php endforeach; ?>
</tbody> </tbody>
</table> </table>
</div> </div>
@@ -242,7 +263,6 @@ $positionsInOrder = sortPlanetsByLongitude($samplePlanets);
<h5 class="table-title">Table 2: Positions in Order</h5> <h5 class="table-title">Table 2: Positions in Order</h5>
<div class="table-responsive"> <div class="table-responsive">
<table class="table table-dark table-bordered align-middle astro-table mb-0"> <table class="table table-dark table-bordered align-middle astro-table mb-0">
<thead> <thead>
<tr> <tr>
<th>Segment</th> <th>Segment</th>
@@ -252,21 +272,22 @@ $positionsInOrder = sortPlanetsByLongitude($samplePlanets);
<th>Nakshatra</th> <th>Nakshatra</th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
<?php if (!empty($positionsInOrder)): ?>
<?php foreach ($positionsInOrder as $planet): ?> <?php foreach ($positionsInOrder as $planet): ?>
<tr>
<td><?= htmlspecialchars($planet['house']) ?></td>
<td><?= htmlspecialchars($planet['name']) ?></td>
<td><?= htmlspecialchars($planet['sign']) ?></td>
<td><?= htmlspecialchars($planet['degree']) ?></td>
<td><?= htmlspecialchars($planet['nakshatra']) ?></td>
</tr>
<?php endforeach; ?>
<?php else: ?>
<tr> <tr>
<td><?= htmlspecialchars($planet['house']) ?></td> <td colspan="5">No ordered planetary data available.</td>
<td><?= htmlspecialchars($planet['name']) ?></td>
<td><?= htmlspecialchars($planet['sign']) ?></td>
<td><?= htmlspecialchars($planet['degree']) ?></td>
<td><?= htmlspecialchars($planet['nakshatra']) ?></td>
</tr> </tr>
<?php endif; ?>
<?php endforeach; ?>
</tbody> </tbody>
</table> </table>
</div> </div>
@@ -280,12 +301,11 @@ $positionsInOrder = sortPlanetsByLongitude($samplePlanets);
</div> </div>
</div> </div>
<script> <script>
const samplePlanets = <?= json_encode($samplePlanets, JSON_UNESCAPED_UNICODE); ?>; const samplePlanets = <?= json_encode($samplePlanets, JSON_UNESCAPED_UNICODE); ?>;
const signNames = <?= json_encode(array_values(SIGN_NAMES[$nameMode])); ?>; const signNames = <?= json_encode(array_values(SIGN_NAMES[$nameMode]), JSON_UNESCAPED_UNICODE); ?>;
</script> </script>
<script src="js.js"></script> <script src="js.js"></script>
</body> </body>
</html> </html>