<?php

/*
   ###############################################
   TEST/DEMO FUNCTION:  Get_Target_Name()

   This program requires an Internet connection to
   access the NASA/JPL Horizons API.

   AUTHOR   : Jay Tanner - 2025
   LANGUAGE : PHP v8.2.12
   LICENSE  : Public Domain
   ###############################################
*/

// --------------------------------------------------
// Define random NASA/JPL body ID for this test/demo.
// It will select a random planet or asteroid.
// Refresh page for new randomized selection.

   
$TargBodyID Random_Int(111);
   
$TargBodyID Str_Replace('1099''10',(($TargBodyID == 11)? '301':$TargBodyID.'99'));
   
$TargBodyID Str_Replace('399''301'$TargBodyID);

   if (
MT_Rand() % == 0) {$TargBodyID Random_Int(150000).';';}


/* -----------------------------------------------
   Override random (TargBodyID) above for testing.
   by uncommenting the line below and entering any
   desired (TargBodyID).   Default = '301' = Moon.
*/

// $TargBodyID = '301';


// ------------------------
// Construct asteroid note.

   
$AsteroidNoteText = (StrPos($TargBodyID';') !== FALSE)? '(Asteroid)' '';


// -----------------------------------------------------------------
// Call function to get the Target Body ID designation text, if any.

   
$TargBodyNameText Get_Target_Name ($TargBodyID);


/* ---------------------------------------------------
   Adjust special characters so that they will display
   properly within an HTML web page.
*/
   
$TargBodyNameText HTMLEntities ($TargBodyNameText);


// ----------------------------------------------------
// Generate client HTML page to display returned result.

   
print <<< _HTML_WEB_PAGE

<!DOCTYPE HTML>

<!-- Top yellow source code view link. --->
<br>
<table width="420" align="bottom" cellspacing="1" cellpadding="3">
<tr>
<td colspan="1" style="font-size:10pt; color:black; background:white;
                       text-align:left;">
<b><a href='View-Source-Code.php' target='_blank'
     style='font-family:Verdana; color:black; background:yellow;
            text-decoration:none; border:1px solid black; padding:4px;'>
&nbsp;View Source Code&nbsp;</a></b>
</td>
</tr>
</table>

<b><pre style='font-size:11pt;'>
TEST/DEMO FUNCTION:

TargBodyNameText = Get_Target_Name (TargBodyID)

This function extracts and returns ONLY the target body name text for any
given body ID# or record #, if such data exists.


REFRESH page for a new random Target Body ID selection.

##############################################################################
NASA/JPL Body ID# or Record#  =  
$TargBodyID     $AsteroidNoteText

-----------------------------------------
Target Body Name, #, or Designation Text:

$TargBodyNameText

##############################################################################
</pre></b>

<!-- For extra scroll space at bottom of page --->
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>

_HTML_WEB_PAGE;


/*
   ###########################################################################
   This function extracts and returns ONLY target body name text for any given
   body ID# or record #, if such data exists.

   Since there are many, many, many thousands of asteroids, there are many of
   them without actual names like Ceres or Vesta, but identified by a variety
   of various catalog symbols that may seem cryptic at first.

   Example: For Target Body ID = 9941;
   Returns: 9941 Iguanodon (1989 CB3)

   NOTE:
   A semicolon (;) at the end of the ID means an asteroid or small body.

   EXAMPLES:

   Given the Target Body ID = 301
   The returned target name string would be:  'Moon (301)'


   Given the Target Body ID = 301;
   The returned target name string would be:  '301 Bavaria (A890 WA)'

   ----------------------------
   Given the asteroid BodyID #;
   2934;

   The returned target name string would be:
   '2934 Aristophanes (4006 P-L)'

   If no target body name is found, then an error message is returned.

   ###########################################################################
*/

   
function Get_Target_Name ($TargBodyID)
{
// ===========================================================================
// Read arguments.

   
$TargBodyID trim($TargBodyID);
   
$Command URLEncode($TargBodyID);

// ---------------------------
// Set to current system date.

   
$StartDate date("Y-M-d");

   
$From_Horizons_API =
   
"https://ssd.jpl.nasa.gov/api/horizons.api?format=text" .
   
"&COMMAND='$Command'"    .
   
"&OBJ_DATA='NO'"         .
   
"&MAKE_EPHEM='YES'"      .
   
"&EPHEM_TYPE='OBSERVER'" ;
// ===========================================================================

/* ----------------------------------------------------------
   Send query to Horizons API to obtain the target body name.
*/
   
$W Str_Replace(",\n"" \n"File_Get_Contents($From_Horizons_API));

/* -----------------------------------
   Check if an ephemeris was returned. If
   not, then return an empty string ('').
*/
   
if (StrPos($W'$$SOE') === FALSE) {return trim($W);}

/* ----------------------------------------------------------
   Get the NASA/JPL Horizons target body name string, if any.
   Not every target body may yet have a fixed name or ID, but
   may possibly still have a record # and ephemeris.
*/
   
$T trim($W);
   
$i StrPos($T'Target body name:');
   
$j StrPos($T'}');
   
$W PReg_Replace("/\s+/"" "trim(substr($T$i$j-$i+1)));
   
$W trim(Str_Replace('Target body name:',''$W));
   
$i StrPos($W'{');

   return 
trim(substr($W0$i));

// End of  Get_Target_Name (...)



?>