SHARE
TWEET

php rss

a guest Nov 26th, 2011 71 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. $numShown = 0;
  4. $maxToShow = 5;
  5. $insideitem = false;
  6. $tag = "";
  7. $title = "";
  8. $description = "";
  9. $link = "";
  10. $locations = array ('http://topics.nytimes.com/topics/reference/timestopics/subjects/e/espionage/index.html?rss=1' );
  11. srand ( ( float ) microtime () * 10000000 ); // seed the random gen
  12. $random_key = array_rand ( $locations );
  13.  
  14. function startElement($parser, $name, $attrs) {
  15.     global $insideitem, $tag, $title, $description, $link;
  16.     if ($insideitem) {
  17.         $tag = $name;
  18.     } elseif ($name == "ITEM") {
  19.         $insideitem = true;
  20.     }
  21. }
  22.  
  23. function endElement($parser, $name) {
  24.     global $insideitem, $tag, $title, $description, $link, $numShown, $maxToShow;
  25.     if ($name == "ITEM") {
  26.        
  27.         if ($numShown >= $maxToShow) {
  28.             return;
  29.         }
  30.        
  31.         $numShown ++;
  32.        
  33.         printf ( "<dt><b><a href='%s' target=new>%s</a></b></dt>", trim ( $link ), htmlspecialchars ( trim ( $title ) ) );
  34.         printf ( "<dt>%s</dt><br><br>", trim ( $description ) );
  35.         printf ( "<div style='clear: both'></div>" );
  36.         $title = "";
  37.         $description = "";
  38.         $link = "";
  39.         $insideitem = false;
  40.     }
  41. }
  42.  
  43. function characterData($parser, $data) {
  44.     global $insideitem, $tag, $title, $description, $link;
  45.     if ($insideitem) {
  46.         switch ($tag) {
  47.             case "TITLE" :
  48.                 $title .= $data;
  49.                 break;
  50.             case "DESCRIPTION" :
  51.                 $description .= $data;
  52.                 break;
  53.             case "LINK" :
  54.                 $link .= $data;
  55.                 break;
  56.         }
  57.     }
  58. }
  59.  
  60. $xml_parser = xml_parser_create ();
  61. xml_set_element_handler ( $xml_parser, "startElement", "endElement" );
  62. xml_set_character_data_handler ( $xml_parser, "characterData" );
  63.  
  64. $fp = fopen ( $locations [$random_key], 'r' ) or die ( "Error reading RSS data." );
  65.  
  66. while ( $data = fread ( $fp, 4096 ) )
  67.     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 ) ) );
  68.  
  69. fclose ( $fp );
  70. xml_parser_free ( $xml_parser );
  71.  
  72. ?>
  73.  
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top