Api Fixed
This commit is contained in:
128
ephemeris_engine.php
Normal file
128
ephemeris_engine.php
Normal 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' => ''
|
||||
];
|
||||
}
|
||||
Reference in New Issue
Block a user