Welcome to Geeklog Wednesday, June 19 2019 @ 06:45 am EDT

Geeklog Forums

NASA "Image of the Day" PHP Block


Status: offline

racooper

Forum User
Junior
Registered: 13/03/04
Posts: 24
Below is some code I hacked on to get a display of NASA's "Image of the Day" into a front page block. It's just a phpblock called with "phpblock_showrssimage". The feed URL is hard-coded, as is the thumbnail image width (at 160). If anyone else finds it useful, great!

PHP Formatted Code
// Block functions designed to show NASA "image of the day"
// original source for rss code from http://forums.digitalpoint.com/showthread.php?t=7354
// Hacked to render image instead of item section
// by RACooper (rac at noidea dot us) 05 SEP 2005

function startElement($parser, $name, $attrs) {
 global $insideitem, $tag, $title, $description, $link, $url, $text;
 if ($insideitem) {
  $tag = $name;
 } elseif ($name == "IMAGE") {
  $insideitem = true;
 }
}

function endElement($parser, $name) {
 global $insideitem, $tag, $title, $description, $link, $url, $text;
 if ($name == "IMAGE") {
  $text  = "<a href="".trim($link)."">".htmlspecialchars(trim($title))."<BR>n";
  $text .= "<img src="".trim($url)."" border="0" width="160"></a>";
  $text .= "<BR>n".htmlspecialchars(trim($description));
  $title = "";
  $description = "";
  $link = "";
  $url = "";
  $insideitem = false;
 }
}

function characterData($parser, $data) {
 global $insideitem, $tag, $title, $description, $link, $url, $text;
 if ($insideitem) {
  switch ($tag) {
   case "DESCRIPTION":
   $description .= $data;
   break;
   case "TITLE":
   $title .= $data;
   break;
   case "URL":
   $url .= $data;
   break;
   case "LINK":
   $link .= $data;
   break;
  }
 }
}

function phpblock_showrssimage() {
global $text;
$insideitem = false;
$tag = "";
$title = "";
$description = "";
$link = "";
$url = "";

$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
$fp = fopen("http://www.nasa.gov/rss/image_of_the_day.rss","r")
 or die("Error reading RSS data.");
while ($data = fread($fp, 4096))
 xml_parse($xml_parser, $data, feof($fp))
  or die(sprintf("XML error: %s at line %d",
   xml_error_string(xml_get_error_code($xml_parser)),  
   xml_get_current_line_number($xml_parser)));
fclose($fp);
xml_parser_free($xml_parser);
return $text;
}

 

Status: offline

geKow

Forum User
Full Member
Registered: 12/01/03
Posts: 445
ok, let's compare it with this block

It didn't work for me so well
My server didn't like the double quotes ("") you used, so I replaced them with single quotes (") and (') pairs.

And finally I understand the difference to the other block

Status: offline

suprsidr

Forum User
Full Member
Registered: 29/12/04
Posts: 555
Location:Champaign, Illinois
I like it

Now we have Astronomy pic o day
and
Nasa pic o day
a little problem in lines 19 and 20 with the quotes, her is the fixed code:

PHP Formatted Code
// Block functions designed to show NASA "image of the day"
// original source for rss code from http://forums.digitalpoint.com/showthread.php?t=7354
// Hacked to render image instead of item section
// by RACooper (rac at noidea dot us) 05 SEP 2005


function startElement($parser, $name, $attrs) {
 global $insideitem, $tag, $title, $description, $link, $url, $text;
 if ($insideitem) {
  $tag = $name;
 } elseif ($name == "IMAGE") {
  $insideitem = true;
 }
}

function endElement($parser, $name) {
 global $insideitem, $tag, $title, $description, $link, $url, $text;
 if ($name == "IMAGE") {
  $text  = "<center><a href=".trim($link).">".htmlspecialchars(trim($title))."<BR>n";
  $text .= "<img src=".trim($url)." border='0' width='160'></a></center>";
  $text .= "<BR>n".htmlspecialchars(trim($description));
  $title = "";
  $description = "";
  $link = "";
  $url = "";
  $insideitem = false;
 }
}

function characterData($parser, $data) {
 global $insideitem, $tag, $title, $description, $link, $url, $text;
 if ($insideitem) {
  switch ($tag) {
   case "DESCRIPTION":
   $description .= $data;
   break;
   case "TITLE":
   $title .= $data;
   break;
   case "URL":
   $url .= $data;
   break;
   case "LINK":
   $link .= $data;
   break;
  }
 }
}

function phpblock_showrssimage() {
global $text;
$insideitem = false;
$tag = "";
$title = "";
$description = "";
$link = "";
$url = "";

$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
$fp = fopen("http://www.nasa.gov/rss/image_of_the_day.rss","r")
 or die("Error reading RSS data.");
while ($data = fread($fp, 4096))
 xml_parse($xml_parser, $data, feof($fp))
  or die(sprintf("XML error: %s at line %d",
   xml_error_string(xml_get_error_code($xml_parser)),  
   xml_get_current_line_number($xml_parser)));
fclose($fp);
xml_parser_free($xml_parser);
return $text;
}


 

FlashYourWeb and Your Gallery with the E2 XML Media Player for Gallery2 - http://www.flashyourweb.com

Status: offline

racooper

Forum User
Junior
Registered: 13/03/04
Posts: 24
dadblamed Forum processor stripped the backslashes out. I use doublequotes so that carriage returns (backslash-n) can be used (singlequotes don't render them).

Let's try it again (without having to do any edits to the post this time
PHP Formatted Code

// Block functions designed to show NASA "image of the day"
// original source for rss code from http://forums.digitalpoint.com/showthread.php?t=7354
// Hacked to render image instead of item section
// by RACooper (rac at noidea dot us) 05 SEP 2005

function startElement($parser, $name, $attrs) {
 global $insideitem, $tag, $title, $description, $link, $url, $text;
 if ($insideitem) {
  $tag = $name;
 } elseif ($name == "IMAGE") {
  $insideitem = true;
 }
}

function endElement($parser, $name) {
 global $insideitem, $tag, $title, $description, $link, $url, $text;
 if ($name == "IMAGE") {
  $text  = "<a href=\"".trim($link)."\">".htmlspecialchars(trim($title))."<BR>\n";
  $text .= "<img src=\"".trim($url)."\" border=\"0\" width=\"160\"></a>";
  $text .= "<BR>\n".htmlspecialchars(trim($description));
  $title = "";
  $description = "";
  $link = "";
  $url = "";
  $insideitem = false;
 }
}

function characterData($parser, $data) {
 global $insideitem, $tag, $title, $description, $link, $url, $text;
 if ($insideitem) {
  switch ($tag) {
   case "DESCRIPTION":
   $description .= $data;
   break;
   case "TITLE":
   $title .= $data;
   break;
   case "URL":
   $url .= $data;
   break;
   case "LINK":
   $link .= $data;
   break;
  }
 }
}

function phpblock_showrssimage() {
global $text;
$insideitem = false;
$tag = "";
$title = "";
$description = "";
$link = "";
$url = "";

$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
$fp = fopen("http://www.nasa.gov/rss/image_of_the_day.rss","r")
 or die("Error reading RSS data.");
while ($data = fread($fp, 4096))
 xml_parse($xml_parser, $data, feof($fp))
  or die(sprintf("XML error: %s at line %d",
   xml_error_string(xml_get_error_code($xml_parser)),  
   xml_get_current_line_number($xml_parser)));
fclose($fp);
xml_parser_free($xml_parser);
return $text;
}
 

All times are EDT. The time is now 06:45 am.

  • Normal Topic
  • Sticky Topic
  • Locked Topic
  • New Post
  • Sticky Topic W/ New Post
  • Locked Topic W/ New Post
  •  View Anonymous Posts
  •  Able to post
  •  Filtered HTML Allowed
  •  Censored Content