Mike Wilcock - WebHelp
NASA Astronomy Picture of the Day
Example Code > Tools & Utilities > NASA Astronomy Picture of the Day

This sample application displays data from the NASA Astronomy Picture of the Day API. The sample application runs under PHP 8.1.

Details of this API and other open API’s provided by NASA can be found here: https://api.nasa.gov/

The API returns the following fields:

Field Name Description
resource A dictionary describing the image_set or planet that the response illustrates, completely determined by the structured endpoint.
concept_tags A boolean reflection of the supplied option. Included in response because of default values.
title The title of the image.
date Date of image. Included in response because of default values.
url The URL of the APOD image or video of the day.
hdurl The URL for any high-resolution image for that day. Returned regardless of ‘hd’ param setting but will be omitted in the response IF it does not exist originally at APOD.
media_type The type of media (data) returned. May either be ‘image’ or ‘video’ depending on content.
explanation The supplied text explanation of the image.
concepts The most relevant concepts within the text explanation. Only supplied if concept_tags is set to True.
thumbnail_url The URL of thumbnail of the video.
copyright The name of the copyright holder.
service_version The service version used.

To fetch the data from the API the application uses a PHP cURL function call, the code for which can be seen below.

function geAPoD($apiKey)
Copy Code
if(empty($apiKey))
{
    return;
}

// Query API.
$ch = curl_init();
$url = "https://api.nasa.gov/planetary/apod?api_key=" . $apiKey;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// Get returned values.
$response = curl_exec($ch);

// Close cURL session.
curl_close($ch);

// Basic error checking
if(curl_errno($ch) !== CURLE_OK)
{
    return;
}

// Encode JSON data as a PHP array.
$data = json_decode($response);

// Create link to image viewer.
$view_link = sc_make_link("control_nasa_apod_image_viewer", p_blank_nasa_apod_image_url=$data->hdurl; p_blank_nasa_apod_image_title=$data->title);

// Load up our field values.
{date} = $data->date;
{explanation} = $data->explanation;
{title} = $data->title;
{thumbnail} = "<a href='" . $view_link . "'><img src='" . $data->hdurl . "' title='" . $data->title . "' width='350'></a>";
{url} = "<img src='" . $data->url . "' title='" . $data->title . "'>";
{hdurl} = "<img src='" . $data->hdurl . "' title='" . $data->title . "'>";
{media_type} = $data->media_type;
{copyright} = $data->copyright;
{img_hyperlink} = "<a href='" . $view_link . "' title='" . $data->title . "'>" . $data->hdurl . "</a>";
See Also