How do those headlines work?

 

In short, it's RSS parsed with PHP.

From the Web:

"RDF Site Summary (RSS) is the name given to a simple and well-established XML format used to syndicate headlines."

"RSS is a lightweight, multipurpose, extensible metadata description and syndication format. RSS is an XML application. RSS is currently used for a number of applications, including news and other headline syndication, weblog syndication, and the propogation of software update lists. It is generally used for any situation when a machine-readable list of textual items and/or metadata about them needs to be distributed."

RSS is a form of XML. RSS files containing news headlines are available from a number of sources -- my source is newsisfree.com. I use the PHP scripts below to retrieve the RSS files containing the headlines, and I used the XML parsing capacity of PHP to extract them. Once the headlines are extracted from the RSS files, they're formatted as HTML and displayed. It's really very simple -- the code I used (below) was just a modified version of the code available here.

 

In the page where the headlines are to appear, I use this:

<?php
include("news_headlines_declarations.php");
$news_src = "http://www.newsisfree.com/HPE/xml/feeds/69/1469.xml";
include("news_headlines.php");

$news_src = "http://www.newsisfree.com/HPE/xml/feeds/96/696.xml";
include("news_headlines.php");
?>

The first include() declares all the functions and classes needed in the subsequent script calls -- only call it once, before you do anything else. Then $news_src is set to the URL of the RSS file -- go to newsisfree.com and find one. The second include() fetches the RSS file, parses it and displays it. To use more than one set of headlines, just set $news_src to the URL of the RSS file and include news_headlines.php for each set -- no need include news_headlines_declarations.php more than before the very first set of headlines.

 

Here is the contents of the two files:

File: news_headlines_declarations.php

<?php

class xItem {
  var 
$xTitle;
  var 
$xLink;
  var 
$xDescription;
}

function 
startElement($parser$name$attrs) {
  global 
$curTag;

  
$curTag .= "^$name";

}

function 
endElement($parser$name) {
  global 
$curTag;

  
$caret_pos strrpos($curTag,'^');

  
$curTag substr($curTag,0,$caret_pos);

}

function 
characterData($parser$data) { 
  global 
$curTag// get the Channel information first
  
global $sTitle$sLink$sDescription;  
  
$titleKey "^RSS^CHANNEL^TITLE";
  
$linkKey "^RSS^CHANNEL^LINK";
  
$descKey "^RSS^CHANNEL^DESCRIPTION";
  if (
$curTag == $titleKey) {
    
$sTitle $data;
  }
  elseif (
$curTag == $linkKey) {
    
$sLink $data;
  }
  elseif (
$curTag == $descKey) {
    
$sDescription $data;
  }

  
// now get the items 
  
global $arItems$itemCount;
  
$itemTitleKey "^RSS^CHANNEL^ITEM^TITLE";
  
$itemLinkKey "^RSS^CHANNEL^ITEM^LINK";
  
$itemDescKey "^RSS^CHANNEL^ITEM^DESCRIPTION";

  if (
$curTag == $itemTitleKey) {
    
// make new xItem    
    
$arItems[$itemCount] = new xItem();     
    
$itemCount++;
    
// set new item object's properties    
    
$arItems[$itemCount 1]->xTitle $data;
  }
  elseif (
$curTag == $itemLinkKey) {
    
$arItems[$itemCount 1]->xLink $data;
  }
  elseif (
$curTag == $itemDescKey) {
    
$arItems[$itemCount 1]->xDescription $data;
    
// increment item counter
    //$itemCount++;
  
}
}

?>

 

File: news_headlines.php

<?php
// ********* General Vars *********
$arItems = array();
$itemCount 0;

// ********* Specify News Source  ************
// $news_src must be set before this file is included in another.
$uFile $news_src;

// ********* PHP XML Stuff **************

// main loop
$xml_parser xml_parser_create();
xml_set_element_handler($xml_parser"startElement""endElement");
xml_set_character_data_handler($xml_parser"characterData");
/*if (!($fp = fopen($uFile,"r"))) {
  die ("could not open RSS for input");
}*/
$fp fopen($uFile,"r");
if(
$fp){
while (
$data fread($fp4096)) {
  if (!
xml_parse($xml_parser$datafeof($fp))) {
    
/*die(sprintf("XML error: %s at line %d", 
xml_error_string(xml_get_error_code($xml_parser)), 
xml_get_current_line_number($xml_parser)));*/
  
}
}
xml_parser_free($xml_parser);
}

// ********* Format Output **************
$newsString1 "";

//for ($i=0;$i<count($arItems);$i++) { use this for all headlines

if(count($arItems) < 5) {$numItems count($arItems);}
else {
$numItems 5;}

for (
$i 0$i $numItems$i++) {
  
  
$txItem $arItems[$i];
  
$newsString1 .= "<a href=\"$txItem->xLink\" 
  class=\"a3\" target=\"_blank\">
$txItem->xTitle</a><br><img 
  src=\"images/news_line.gif\" height=\"12\" 
  width=\"147\"><br>"
;
  
}

echo 
$newsString1;

?>