TinyPortal

Development => Block Codes => Topic started by: Max on July 26, 2006, 03:58:25 AM

Title: Pulling RSS Feed Into An PHP Article
Post by: Max on July 26, 2006, 03:58:25 AM
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.
Title: Re: Pulling RSS Feed Into An PHP Article
Post by: Sledge on October 10, 2006, 04:10:22 AM
WooHoo! Just what I've been looking for! I'm gonna try it right now.
Title: Re: Pulling RSS Feed Into An PHP Article
Post by: Balrost on December 22, 2006, 09:25:55 PM
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?
Title: Re: Pulling RSS Feed Into An PHP Article
Post by: domineaux on December 26, 2006, 05:12:06 PM
I'd like to see a demo or site where this is happening. 

Thanks
Title: Re: Pulling RSS Feed Into An PHP Article
Post by: Nokonium on December 26, 2006, 05:51:54 PM
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).
Title: Re: Pulling RSS Feed Into An PHP Article
Post by: Balrost on December 26, 2006, 07:46:22 PM
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.
Title: Re: Pulling RSS Feed Into An PHP Article
Post by: latinlives on January 11, 2007, 10:26:38 PM
How can I limit the number of the articles displayed?
Title: Re: Pulling RSS Feed Into An PHP Article
Post by: srm on March 20, 2007, 09:11:46 PM
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.
Title: Re: Pulling RSS Feed Into An PHP Article
Post by: Lesmond on March 20, 2007, 09:29:04 PM
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);
Title: Re: Pulling RSS Feed Into An PHP Article
Post by: srm on March 20, 2007, 11:08:10 PM
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.
Title: Re: Pulling RSS Feed Into An PHP Article
Post by: sanax on March 28, 2007, 06:28:48 AM
Quote from: Lesmondâ„¢ on March 20, 2007, 09:29:04 PM
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);


This code works perfect! Any idea how I can set the items to show? At the moment it shows all the items available in the feed. Thx
Title: Re: Pulling RSS Feed Into An PHP Article
Post by: majo on March 30, 2007, 06:41:20 PM
Nice work guy , i installed it on my site,

Demo :
http://www.ilovetoargue.com/popular_blog-p10.html
Title: Re: Pulling RSS Feed Into An PHP Article
Post by: srm on March 30, 2007, 06:42:45 PM
IM still waiting for someone to tell me where the hell I find rss instead of xml. All the feeds i have found relative to my genre are all in xml
Title: Re: Pulling RSS Feed Into An PHP Article
Post by: rebelrose on March 30, 2007, 07:35:39 PM
Quote from: srm on March 30, 2007, 06:42:45 PM
IM still waiting for someone to tell me where the hell I find rss instead of xml. All the feeds i have found relative to my genre are all in xml

We are sorry if your questions are not answered fast enough for you. We try the best we can to answer all questions, however if we are too slow you can ry the SMF site for your question.

Thank You.
Title: Re: Pulling RSS Feed Into An PHP Article
Post by: srm on March 30, 2007, 07:36:40 PM
i never said anything about response time . Im still waiting patiently.
Title: Re: Pulling RSS Feed Into An PHP Article
Post by: majo on March 30, 2007, 09:57:15 PM
Have you tried rss feeder, topix google ? google usualy puts out in RSS instead of xml ?

what is your topic i might be able to do better research ?
Title: Re: Pulling RSS Feed Into An PHP Article
Post by: srm on March 30, 2007, 11:34:36 PM
well if ound a page with tons of xml feeds that where about the dance music scene and the latest dance and electronica music news like charts latest releases etc. but they are all xml
Title: Re: Pulling RSS Feed Into An PHP Article
Post by: superQ on March 30, 2007, 11:52:47 PM
I have this on my computer. I never used it though

http://www.extralabs.net/

it is the rss feed editor. as you can see in the attach it says it can edit xml feeds to rss. I do not know how it works but it might be worth a try,since what you are looking for seems to be all xml...
Title: Re: Pulling RSS Feed Into An PHP Article
Post by: hawke on April 05, 2007, 04:29:31 AM
Im not sure this will help you out.  I am using xml feeds in a script box and they seem to work fine the code for the weather channel is

<div id="wx_module_737">
   39564 (Ocean Springs Weather Forecast, MS) (http://www.weather.com/weather/local/39564)
</div>

<script type="text/javascript">

   /* Locations can be edited manually by updating 'wx_locID' below.  Please also update */
   /* the location name and link in the above div (wx_module) to reflect any changes made. */
   var wx_locID = '39564';

   /* If you are editing locations manually and are adding multiple modules to one page, each */
   /* module must have a unique div id.  Please append a unique # to the div above, as well */
   /* as the one referenced just below.  If you use the builder to create individual modules  */
   /* you will not need to edit these parameters. */
   var wx_targetDiv = 'wx_module_737';

   /* Please do not change the configuration value [wx_config] manually - your module */
   /* will no longer function if you do.  If at any time you wish to modify this */
   /* configuration please use the graphical configuration tool found at */
   /* https://registration.weather.com/ursa/wow/step2 */
   var wx_config='SZ=180x150*WX=FHW*LNK=SSNL*UNT=F*BGI=spring*MAP=null|null*DN=www.mgcdadarts.com*TIER=0*PID=1035884805*MD5=e7ec93fc04cdf7cc1d9e716a6d8a2826';

   document.write('<scr'+'ipt src="'+document.location.protocol+'//wow.weather.com/weather/wow/module/'+wx_locID+'?config='+wx_config+'&proto='+document.location.protocol+'&target='+wx_targetDiv+'"></scr'+'ipt>'); 
</script>

you can see it at www.mgcdadarts.com
Title: Re: Pulling RSS Feed Into An PHP Article
Post by: Xildjian on April 14, 2007, 07:01:16 PM
Quote from: Lesmondâ„¢ on March 20, 2007, 09:29:04 PM
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);


How would one go about getting this to work if the feed is not linked to a specific .rss or .xml file? I've tried the different rss readers posted in the forums, but none of them seem to work with this URL: http://st.flobbit.com/feed/atom/

THanks
Title: Re: Pulling RSS Feed Into An PHP Article
Post by: JSW on October 04, 2009, 06:56:10 AM
With this code is it possible to add the scrolling feature? I like that this uses the actual .rss extension because the links work. I've tried the other rss feed codes but the links don't link properly. I've tried to add the scroll code myself to this code but I'm a newbie and it don't work. So any help would be appreciated. Thanks

Quote from: Lesmondâ„¢ on March 20, 2007, 09:29:04 PM
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);

Title: Re: Pulling RSS Feed Into An PHP Article
Post by: Freddy on October 04, 2009, 01:06:36 PM
Do you mean a marquee ? LIke this :

News..
Title: Re: Pulling RSS Feed Into An PHP Article
Post by: JSW on October 04, 2009, 07:52:39 PM
No, sorry for not explaining properly.

I want that code I posted because it posts rrs feeds properly and links properly, unlike the other rrs codes that I've tried. I put this code into a block and it works perfectly. The links all work. The only issue is that the block box is huge. I'd like to be able to control it in a block box that is 200 in height and have it scroll up. I'm not sure if that's possible to do with this code but if so, it'd be perfect for me.

The other rrs codes I've tried on here work but the links don't work. They try to link within my website url and when clicked they give an error. I'm using rrs feeds from a twitter account.
Title: Re: Pulling RSS Feed Into An PHP Article
Post by: Freddy on October 04, 2009, 08:36:29 PM
Try this :

// An RSS Reader to grab news from whichever site you choose
$backend = "http://newsrss.bbc.co.uk/rss/newsonline_uk_edition/technology/rss.xml"; //change this to match the rss feed url you wish to display.
// end

// 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);

echo '
<div style="height: 200px;width: 100%;overflow-x:hidden;overflow-y:auto">';

// 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)));
}

echo '
</div>';

// Close the datafile

fclose($fp);

// Free any memmory used

xml_parser_free($xml_parser);



You don't seem to be using that endtheElement function so I removed it.  Easy enough to put it back in...   I put the whole output inside a DIV and gave you scrolling on the right hand side.

Hope that's what you meant :)