RSS Integration (External FEEDS ONLY)

If you have created additional (non official) documentation or tutorials or something like that please post this here
Post Reply
trip
Posts: 657
Joined: Tue 17. Feb 2004, 09:56
Location: Cape Town, South Africa
Contact:

RSS Integration (External FEEDS ONLY)

Post by trip »

Added from
http://www.phpwcms.de/forum/viewtopic.php?t=1596
===============================================
Okay here is what I did

NOTE: I commented out several functions for my USE

using the TAG {PHP:your_dir/with_rss_file_below.php}

save the following below as a php file.
I have commented out several lines, but if you uncomment this then you have extended functions.

change the following
"http://news.bbc.co.uk/rss/newsonline_uk ... rss091.xml" => "uk",
with the url of your rss feed, I have 2 feeds there, so you need to delete whichever for this note to be random.
note: this is an array for a random picker, see the file, and play with this.
note: this is not my coding, and might be improved in the future

I find this script works well, there are also no conflicts with the script...
enjoy
regards
TriP.

Code: Select all

<?php
/*
* PHP RSS parser 0.1
* ------------------
*
* RSS is RDF Site Summary. A technology used by netscape
* in their browser's channel function.
*
* This script gives you a PHP interface to read such
* feeds using the XML functions of PHP.
*
* Future changes:
*  - A cachefunction to prevent putting heavy load on
*    the server in the other end. Also to give a lot
*    shorter loading time.
*
* Author: Christian J&oslash;rgensen
*         http://www.razor.dk
*         mail@phpguru.dk
*/

/*function select_box($array, $selected, $name) {

  // This is my nifty selectbox-function :)

  $out = "<select name=\"$name\">";
  foreach ($array as $key => $value) $out .= sprintf("<option value=\"$key\"%s>$value</option>\n", $key == $selected ? ' selected' : '');
  return $out."</select>";    

}
*/
/*
* This is the dataarray containing the URLs and Names of the
* feeds to fetch. The URL goes as key.
*/

$backends2 = array(
          
"http://news.bbc.co.uk/rss/newsonline_uk_edition/england/rss091.xml" => "uk",
"http://newstrove.com/cgi-bin/rss.xml?search=austria&context_words=course+austrian+europe&max_results=20" => "at",
          );

if (!$backend2) {

  // If no backend is chosen, chose a random

  srand((float) microtime() * 10000000);
  $backend2 = array_rand($backends2);

}

// print "<h1>".$backends[$backend]."</h1>";
// The form containing the selectbox

// print "<form action='$PHP_SELF' method='POST'>";
// print select_box($backends, $backend, "backend");
// print "&nbsp;<input type='submit'>";
// print "</form>";

// Some vars used later on

$insideitem = false;
$tag = "";
$title = "";
$description = "";
$link = "";
$catagory = "";

function startElement2($parser, $tagName, $attrs) {

  // The function used when an element is encountered

  global $insideitem, $tag;

  if ($insideitem) {

    $tag = $tagName;

  } elseif ($tagName == "ITEM") {

    $insideitem = true;
  }

}

function characterData2($parser, $data) {

  // The function used to parse all other data than tags

  global $insideitem, $tag, $title, $description, $link, $catagory;
  
  if ($insideitem) {
  
    switch ($tag) {
    case "TITLE":
      $title .= $data;
      break;
    case "DESCRIPTION":
      $description .= $data;
      break;
    case "LINK":
      $link .= $data;
      break;
	 case "CATAGORY":
	  $catagory .= $data;
	  break;
    }

  }

}

function endElement2($parser, $tagName) {

  // This function is used when an end-tag is encountered.

  global $insideitem, $tag, $title, $description, $link, $catagory;
  
  if ($tagName == "ITEM") {
  
  printf("<b>• <a href='%s' target='_blank'>%s</a></b><br>",
       trim($link),htmlspecialchars(trim($title)));
    //printf("%s<br><br>",htmlspecialchars(trim($description)));
    $title = $description = $link = $insideitem = $catagory = false;

  }

}

// Now to the parsing itself. Starts by creating it:

$xml_parser = xml_parser_create();

// Then setup the handlers:
// CHange the Functions to match the new ones above

xml_set_element_handler($xml_parser, "startElement2", "endElement2");
xml_set_character_data_handler($xml_parser, "characterData2");

// Open the actual datafile:
$fp = fopen($backend2, r);

// Run through it line by line and parse:
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)));
}

// Close the datafile
fclose($fp);

// Free any memmory used
xml_parser_free($xml_parser);

?>
frold
Posts: 2151
Joined: Tue 25. Nov 2003, 22:42

Post by frold »

cool I will test it :D
http://www.studmed.dk Portal for doctors and medical students in Denmark
trip
Posts: 657
Joined: Tue 17. Feb 2004, 09:56
Location: Cape Town, South Africa
Contact:

Post by trip »

I am not sure if the cache thing works
maybe someone has an idea...however for my needs this is working really well...
Just make sure you select good rss feeds...

TriP.
submike
Posts: 50
Joined: Wed 25. Aug 2004, 17:36
Location: Chicago
Contact:

Post by submike »

Couple questions/comments on this code

Comments

When using the selectbox, change the following code from:

Code: Select all

// print select_box($backends, $backend, "backend");
to

Code: Select all

// print select_box($backends2, $backend, "backend");
the array is called $backends2, not $backends - saves some debug time


Questions

For the selectbox, the following code does not seem to work

Code: Select all

// print "<form action='$PHP_SELF' method='POST'>";
What action should I call in the form section?
trip
Posts: 657
Joined: Tue 17. Feb 2004, 09:56
Location: Cape Town, South Africa
Contact:

Post by trip »

The code that you changed had been commented out, as I did not need it...if you took all the changes out, you will see that the code has a drop down menu, where you can select the different rss feeds.

The integration into the site has been really slick...and if there is any conflict it has to do with the "$backend2" that needs to be changed to another variable.

Comment on the form.
If you changed the code that you changed to the correct variable then the form should work. Dont forget to uncomment all the areas which I commented out...otherwise this will not work.

TriP
submike
Posts: 50
Joined: Wed 25. Aug 2004, 17:36
Location: Chicago
Contact:

Post by submike »

Here is the code I'm currently using, and I'm still having problems. The drop down does not seem to work, as it just refreshes the page and randomly picks a feed. I think it has something to do with the form action. Any help would be appreciated.

You can check out the code at http://cms.itcweb.org/index.php?news

Code: Select all

<?php
/*
* PHP RSS parser 0.1
* ------------------
*
* RSS is RDF Site Summary. A technology used by netscape
* in their browser's channel function.
*
* This script gives you a PHP interface to read such
* feeds using the XML functions of PHP.
*
* Future changes:
*  - A cachefunction to prevent putting heavy load on
*    the server in the other end. Also to give a lot
*    shorter loading time.
*
* Author: Christian J&oslash;rgensen
*         http://www.razor.dk
*         mail@phpguru.dk
*/

function select_box($array, $selected, $name) {
  // This is my nifty selectbox-function :)
  $out = "<select name=\"$name\" class=\"selectMenu\">\n";
  foreach ($array as $key => $value) $out .= sprintf("  <option value=\"$key\"%s>$value</option>\n", $key == $selected ? ' selected' : '');
  return $out."</select>\n";   
}

/*
* This is the dataarray containing the URLs and Names of the
* feeds to fetch. The URL goes as key.
*/

$backends2 = array(         
	"http://rss.lstech.org/get/v10/ls/55.rss" => "Calendar Events",
	"http://rss.lstech.org/get/v10/ls/51.rss" => "Job Listings",
 );

if (!$backend2) {
  // If no backend is chosen, chose a random
  srand((float) microtime() * 10000000);
  $backend2 = array_rand($backends2);
}

// The form containing the selectbox
print "<form action='$PHP_SELF' method='POST'>\n";
print "<p>Select a newsfeed<br><br>\n";
print select_box($backends2, $backend, "backend");
print "&nbsp;<input type='submit' value='Select' class='formButtonItc'>\n";
print "</p></form>\n<p>";


// Some vars used later on
$insideitem = false;
$tag = "";
$title = "";
$description = "";
$link = "";
$catagory = "";


function startElement2($parser, $tagName, $attrs) {
  // The function used when an element is encountered
  global $insideitem, $tag;
  if ($insideitem) {
    $tag = $tagName;
  } elseif ($tagName == "ITEM") {
    $insideitem = true;
  }
}


function characterData2($parser, $data) {
  // The function used to parse all other data than tags
  global $insideitem, $tag, $title, $description, $link, $catagory;
  if ($insideitem) {
    switch ($tag) {
    case "TITLE":
      $title .= $data;
      break;
    case "DESCRIPTION":
      $description .= $data;
      break;
    case "LINK":
      $link .= $data;
      break;
    case "CATAGORY":
     $catagory .= $data;
     break;
    }
  }
}


function endElement2($parser, $tagName) {
  // This function is used when an end-tag is encountered.
  global $insideitem, $tag, $title, $description, $link, $catagory;
  if ($tagName == "ITEM") {
 	printf("• <a href='%s' target='_blank'>%s</a><br>",
    trim($link),htmlspecialchars(trim($title)));
    //printf("%s<br><br>",htmlspecialchars(trim($description)));
    $title = $description = $link = $insideitem = $catagory = false;
  }
}

// Now to the parsing itself. Starts by creating it:

$xml_parser = xml_parser_create();

// Then setup the handlers:
// CHange the Functions to match the new ones above

xml_set_element_handler($xml_parser, "startElement2", "endElement2");
xml_set_character_data_handler($xml_parser, "characterData2");

// Open the actual datafile:
$fp = fopen($backend2, r);

// Run through it line by line and parse:
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)));
}

// Close the datafile
fclose($fp);

// Free any memmory used
xml_parser_free($xml_parser);

?> 
trip
Posts: 657
Joined: Tue 17. Feb 2004, 09:56
Location: Cape Town, South Africa
Contact:

Post by trip »

Try commenting out this

Code: Select all

if (!$backend2) {
  // If no backend is chosen, chose a random
  srand((float) microtime() * 10000000);
  $backend2 = array_rand($backends2);
} 
Have not tested if this function is disabled what will happen, but give it a go..

BTW, cool looking site...
TriP
submike
Posts: 50
Joined: Wed 25. Aug 2004, 17:36
Location: Chicago
Contact:

Post by submike »

Thanks for responding and the comment on the site. I tried commenting out the code above and received the following error.

Warning: fread(): supplied argument is not a valid stream resource in /hsphere/local/home/itcadmin/cms.itcweb.org/php/rss_news.php on line 123

Warning: fclose(): supplied argument is not a valid stream resource in /hsphere/local/home/itcadmin/cms.itcweb.org/php/rss_news.php on line 131

I'm relatively new to php and php wcms, so it could be something I'm overlooking. Thanks in advance for your help.
trip
Posts: 657
Joined: Tue 17. Feb 2004, 09:56
Location: Cape Town, South Africa
Contact:

Post by trip »

Good morning
I will look into this next week...up to my ears in work...
TriP
Post Reply