TP-Docs
HTML5 Icon HTML5 Icon HTML5 Icon
TP on Social Media

Recent

Welcome to TinyPortal. Please login or sign up.

March 29, 2024, 08:33:33 AM

Login with username, password and session length
Members
Stats
  • Total Posts: 195,106
  • Total Topics: 21,213
  • Online today: 358
  • Online ever: 3,540 (September 03, 2022, 01:38:54 AM)
Users Online

Pulling RSS Feed Into An PHP Article

Started by Max, July 26, 2006, 03:58:25 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Max

Found an eay way to display RSS Feeds in a PHP Article  :)

Copy this code below into a PHP Article....


// TP RSS Article Script

// TP Function
$context['TPortal']['rss_notitles']= false;

// Create An XML Parser
$xml_parser = xml_parser_create();

// Set The Functions To Handle Opening & Closing Tags
xml_set_element_handler($xml_parser, "startElement", "endElement");

// Set The Function To Handle Blocks Of Character Data
xml_set_character_data_handler($xml_parser, "characterData");

// Open The XML File
$fp = fopen("http://news.bbc.co.uk/rss/newsonline_world_edition/front_page/rss.xml","r")
or die("Error reading RSS data.");

// Read The XML File 4KB At A Time
while ($data = fread($fp, 4096))

// Parse Each 4KB Chunk With The XML Parser
xml_parse($xml_parser, $data, feof($fp))

// Handle errors in parsing
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 XML File
fclose($fp);

// Free Up Memory Used By XML Parser
xml_parser_free($xml_parser);


Edit:
Moved To Block code snippets to make it easier to find.

Sledge

WooHoo! Just what I've been looking for! I'm gonna try it right now.

Balrost

Hi. 

Thanx for the code works pretty good and fairly simple for us non coders. LOL  One question tho.

The code is not showing all the data from the xml doc.  Here is a sample of the xml

<item>
<title>ArkInventory (1.22)</title>
?
<link>
http://www.wowinterface.com/downloads/info6488-1.22.html
</link>
?
<description>
Overview:

unlimited number of bars (there are practical limits though before your screen becomes full)
assign items to a category of your choice (overrides the default assignment)
...
</description>
<author>arkayenro</author>
<category domain="http://www.wowinterface.com"/>
?
<guid>
http://www.wowinterface.com/downloads/info6488-1.22.html
</guid>
<pubDate>Fri, 22 Dec 2006 13:54:55</pubDate>
</item>


The tile shows fine, the links works, the decription shows up, but the pupDate and the author are missing.  The catagory and guid are missing as well but I dont need that info.

Any ideas of how to make these show in the rss article?

domineaux

I'd like to see a demo or site where this is happening. 

Thanks

Nokonium

#4
There is another thread on this topic

http://www.tinyportal.net/index.php?topic=11569.msg96623#msg96623

Examples of my version can be found here http://nokonium.co.uk/Fiddlers-Elbow/index.php bottom of the left sidebar (this is one of my test sites).

Balrost

The rss feed can be viewed at www.hgguild.com.  In the left box area is a article called Balrot's UI rss feed.  It will bring up the article and you can see it is missing some of the data.  It uses the XML code format I posted above.

latinlives

How can I limit the number of the articles displayed?

srm

Ok Im still confused about this rss thing.

what I am trying to do is use rss feeds of the latest dance music news etc. I found a few places but they are all xml urls.

When I am making a block and I want the rss feed to display properly with working links to the articles etc. should i be putting xml urls? into the rss feed block?

example: I want some of the feeds from this page: http://chordata.info/-/104/    in a center block and also i want to be able to put some feeds just in articles only..

Anyone that can give me a few basic answers here I greatly thank you.

Lesmond

I have a PHP version here you could try works in any block or article, not tested it with xml feeds though

// An RSS Reader to grab news from whichever site you choose
$backend = "Place feed here inside the qoutes "; //change this to match the rss feed url you wish to display.
// end


function endtheElement($parser, $tagName) {
// This function is used when an end-tag is encountered.
global $insideitem, $tag, $title, $description, $link;
 
  if ($tagName == "ITEM") {
    echo '<table width="100%" border="0"><tr>
      <td class="main-table">
      <span class="nav-head"><strong>
      ';
    printf("<p><b><a href='%s'>%s</a></b></p>", // make our title into an actual link
    trim($link),htmlspecialchars(trim($title))); // remove html characters from the title
 
    echo "</tr>";
    echo '<tr>
      <td class="nav">';
    printf("<p>%s</p>",$description); // Print out the live journal entry
    echo"</td>";
    echo "</tr>";
    echo "</table>";
    echo "<br>";
    $title = $description = $link = $insideitem = false;
   }
}

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

$xml_parser = xml_parser_create();

// Then setup the handlers:

xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");

// Open the actual datafile:

$fp = fopen($backend, 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);

srm

so i use .rss urls for that? thats the think all the rss feeds i keep finding are in xml urls. so im a bit confused do i put in the xml url or do i need to find an rss url and put those in the block.

anyways i guess ill try both.