Jump to content

Recommended Posts

Posted

I did this as a favor for someone, but I figured I'd share it with everyone.. you know, spread the wealth.

This code simply reads the RSS feed that SMF outputs (of the latest posts) and returns it as a list (with the option to make it linkable).

The implementation technique can vary, you could include the file, or just copy and paste it into your page (~100 lines).

<?php

/**

SMF Forum RSS Feed to Latest Posts List

Built for phpVMS
www.phpvms.net

Built by Lorenzo Aiello
www.la92.com

**/

/********* CONFIGURATION *********/

// Full URL to your SMF installation, WITH trailing slash
$board_url = 'http://forum.yourwebsite.com/';

// Want the list to be linkable? TRUE = Yes, FALSE = No
$linkable = TRUE;

/******* END CONFIGURATION *******/


/**

== STOP ==

DO

NOT

EDIT

BELOW

THIS

**/

function xml2ary(&$string) {
   $parser = xml_parser_create();
   xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
   xml_parse_into_struct($parser, $string, $vals, $index);
   xml_parser_free($parser);

   $mnary=array();
   $ary=&$mnary;
   foreach ($vals as $r) {
       $t=$r['tag'];
       if ($r['type']=='open') {
           if (isset($ary[$t])) {
               if (isset($ary[$t][0])) $ary[$t][]=array(); else $ary[$t]=array($ary[$t], array());
               $cv=&$ary[$t][count($ary[$t])-1];
           } else $cv=&$ary[$t];
           if (isset($r['attributes'])) {foreach ($r['attributes'] as $k=>$v) $cv['_a'][$k]=$v;}
           $cv['_c']=array();
           $cv['_c']['_p']=&$ary;
           $ary=&$cv['_c'];

       } elseif ($r['type']=='complete') {
           if (isset($ary[$t])) { // same as open
               if (isset($ary[$t][0])) $ary[$t][]=array(); else $ary[$t]=array($ary[$t], array());
               $cv=&$ary[$t][count($ary[$t])-1];
           } else $cv=&$ary[$t];
           if (isset($r['attributes'])) {foreach ($r['attributes'] as $k=>$v) $cv['_a'][$k]=$v;}
           $cv['_v']=(isset($r['value']) ? $r['value'] : '');

       } elseif ($r['type']=='close') {
           $ary=&$ary['_p'];
       }
   }    

   _del_p($mnary);
   return $mnary;
}

// _Internal: Remove recursion in result array
function _del_p(&$ary) {
   foreach ($ary as $k=>$v) {
       if ($k==='_p') unset($ary[$k]);
       elseif (is_array($ary[$k])) _del_p($ary[$k]);
   }
}

$feed = file_get_contents($board_url.'index.php?type=rss;action=.xml');
$feed = xml2ary($feed);

$feed = $feed['rss']['_c']['channel']['_c'];

foreach($feed['item'] as $item)
{
if($linkable)
{
	echo '<li><a href="'.$item['_c']['link']['_v'].'" target="_blank">'.$item['_c']['title']['_v'].'</a></li>';
}else{
	echo '<li>'.$item['_c']['title']['_v'].'</li>';
}
}

?>

  • Like 1
Posted

As a brief update..

A bug I found is that you will need at least two posts in your forum for this to work propery.

Also, if you get errors like this:

Warning: file_get_contents() [function.file-get-contents]: URL file-access is disabled in the server configuration in /home/XXXXX/public_html/lib/skins/XXXXX/frontpage_main.tpl on line XXXX

It means that your web hosting provider is a little paranoid with their PHP security, so you can use this code instead, which uses a different method of retrieving the information.

<?php

/**

SMF Forum RSS Feed to Latest Posts List

Built for phpVMS
www.phpvms.net

Built by Lorenzo Aiello
www.la92.com

**/

/********* CONFIGURATION *********/

// Full URL to your SMF installation, WITH trailing slash
$board_url = 'http://forum.yourwebsite.com/';

// Want the list to be linkable? TRUE = Yes, FALSE = No
$linkable = TRUE;

/******* END CONFIGURATION *******/


/**

== STOP ==

DO

NOT

EDIT

BELOW

THIS

**/

function xml2ary(&$string) {
   $parser = xml_parser_create();
   xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
   xml_parse_into_struct($parser, $string, $vals, $index);
   xml_parser_free($parser);

   $mnary=array();
   $ary=&$mnary;
   foreach ($vals as $r) {
       $t=$r['tag'];
       if ($r['type']=='open') {
           if (isset($ary[$t])) {
               if (isset($ary[$t][0])) $ary[$t][]=array(); else $ary[$t]=array($ary[$t], array());
               $cv=&$ary[$t][count($ary[$t])-1];
           } else $cv=&$ary[$t];
           if (isset($r['attributes'])) {foreach ($r['attributes'] as $k=>$v) $cv['_a'][$k]=$v;}
           $cv['_c']=array();
           $cv['_c']['_p']=&$ary;
           $ary=&$cv['_c'];

       } elseif ($r['type']=='complete') {
           if (isset($ary[$t])) { // same as open
               if (isset($ary[$t][0])) $ary[$t][]=array(); else $ary[$t]=array($ary[$t], array());
               $cv=&$ary[$t][count($ary[$t])-1];
           } else $cv=&$ary[$t];
           if (isset($r['attributes'])) {foreach ($r['attributes'] as $k=>$v) $cv['_a'][$k]=$v;}
           $cv['_v']=(isset($r['value']) ? $r['value'] : '');

       } elseif ($r['type']=='close') {
           $ary=&$ary['_p'];
       }
   }    

   _del_p($mnary);
   return $mnary;
}

// _Internal: Remove recursion in result array
function _del_p(&$ary) {
   foreach ($ary as $k=>$v) {
       if ($k==='_p') unset($ary[$k]);
       elseif (is_array($ary[$k])) _del_p($ary[$k]);
   }
}

$curl_handle=curl_init();
curl_setopt($curl_handle,CURLOPT_URL,$board_url.'index.php?type=rss;action=.xml');
curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,2);
curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1);
$feed = curl_exec($curl_handle);
curl_close($curl_handle);

$feed = xml2ary($feed);

$feed = $feed['rss']['_c']['channel']['_c'];

foreach($feed['item'] as $item)
{
if($linkable)
{
	echo '<li><a href="'.$item['_c']['link']['_v'].'" target="_blank">'.$item['_c']['title']['_v'].'</a></li>';
}else{
	echo '<li>'.$item['_c']['title']['_v'].'</li>';
}
}

?>

  • 4 weeks later...
Posted

i use the 1st code but

Warning: file_get_contents() [function.file-get-contents]: Couldn't resolve host name in /home/greeceai/public_html/fss/lib/skins/crystalogre/frontpage_main.tpl on line 112

Warning: file_get_contents(http://forum.yourwebsite.com/index.php?type=rss;action=.xml) [function.file-get-contents]: failed to open stream: operation failed in /home/greeceai/public_html/fss/lib/skins/crystalogre/frontpage_main.tpl on line 112

Warning: Invalid argument supplied for foreach() in /home/greeceai/public_html/fss/lib/skins/crystalogre/frontpage_main.tpl on line 117

the 2nd code

Warning: Invalid argument supplied for foreach() in /home/greeceai/public_html/fss/lib/skins/crystalogre/frontpage_main.tpl on line 123

the line 123 is :

foreach($feed['item'] as $item)

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...