PDA

View Full Version : How to implement "recent blog posts" on home page?



Mr. Pink
10-01-2009, 07:45 AM
My question is very quick...

I see that this site has a section that automatically updates itself and lists "recent blog posts" on the home page.

How is this done?

Note: My blog is hosted separately form my site (through Blogger).


Thanks...

Selkirk
10-01-2009, 08:49 AM
If you want to go the PHP route, try SimplePie (http://simplepie.org/wiki/tutorial/start).

Blue Cat Buxton
10-01-2009, 09:23 AM
I use the script here... http://boastology.com/pages/mods.php ... the one at the bottom

You can see it in action here (slightly modified output) http://www.cameraphonereview.co.uk towards the bottom under latest cameraphone news

It works with different domains, so should fit the bill.

I also just looked at simplepie, but the demo did not seem to be working.

Chris
10-01-2009, 01:22 PM
I use a generic RSS posting script. IT takes any RSS feed and can post it, I modified it as needed and just feed my blog's rss feed to it.

That type of setup will work for any blog, remotely hosted or otherwise.

Mr. Pink
10-02-2009, 09:35 AM
Thanks, everyone...

Before I do any work on this, one more quick question.

Since my blog is remotely hosted, am I doing my main site a disfavor by having these outgoing links now? I know some of the PR will get sent back to my main site (page) because the blog has links to it, but I also know that some of the PR still gets lost.

So, do I stand to gain more form having this feature, than I stand to lose by leaking part of my PR?

What would be the general opinion on this issue?

Thanks...

Chris
10-02-2009, 01:12 PM
I wouldn't worry.

Your blog is remotely hosted, but that doesn't mean you do not control it. PageRank flows irregardless of domain, and in the grand scheme of things your blog will function, for PR flow, as if it were not remotely hosted and just another part of your site. Assuming you'll of course have links from your blog back to your site.

Mr. Pink
10-04-2009, 10:22 PM
I'm afraid I'm not able to make any progress on this.

I looked at SimplePie but the whole thing was just confusing and unclear to me. I don't see any clear instructions on that site, that would tell me how to do this.

I downloaded http://boastology.com/pages/mods.php as Blue Cat Buxton suggested. I would really like to make this work, but there are absolutely no instructions for non-programmer like myself. It says the script is heavily commented, but to me its all Greek. There is one line that says "Enter the url to an RSS feed". I entered my feed's URL there. Then I uploaded that scrip to my server and tried to access it with my browser, to see what happens, and all I got was an error message. In any event, I would like to have the last posts of my blog appear on my home page, which is a static HTML page, and there are no instructions that explain how to do that (and I read all the comments in the script).

I also did a Google search for "RSS posting script" as Chris suggested, but wasn't able to make any sense of anything I found.

I'm kind of stuck...

Any help...? :confused:

Blue Cat Buxton
10-05-2009, 01:38 AM
What was the error you got? I may be able to help.

Post here or send me a PM and I will see what I can do.

Mr. Pink
10-05-2009, 10:24 AM
Thanks for looking into this.

The error message is "unclosed token".

The script is this:


<?php

/*##############################################

RSS parser written for bMachine feed.
This script can be used to read ANY kind of RSS feeds from anywhere!


*--------------------------------------------*
Written by Kailash Nadh,
http://bnsoft.net , kailash@bnsoft.net

Author of bMachine, http://boastology.com

I wrote this script on the 1st day I studied
XML by running through the documentation at www.php.net ;)
*--------------------------------------------*

WARNING!: This script is Heavily commented! :)


This is how the script works.
> Reads the XML file
> Parses it into an array using xml_parse_into_struct()
> Converts that array into a more sensible, easily usable array
[ See the structure.txt file to get an idea of the array structure ]
> Finally, Display the data in anyway you want!

This script demonstrates the use of simple logic
to do Complex XML/RSS parsing functions
This script can be easily developed into a powerful application.

##############################################*/



$url="http://feeds.feedburner.com/cardshark";
// Enter the url to an RSS feed

$data=@fread(fopen("$url","r"),10000) or die("Cant open $url!");
// Get the file contents

$myar=getXmlData($data);
// What ever it may be, the argument to this
// function should be the XML document's content


// Now $myar holds the fully parsed XML contents.
// It may have a number of tags, like <TITLE> , <LINK> , <AUTHOR> , <SOMETHING> ... and so on
// With a simple loop, you can extract all the tags and their values neatly

// Remember, $i<= should be lesser than or equal to the number of values
// a tag has. If you give count($myar), it will only take the number of tags.
// See structure.txt for better understanding.


for($i=0;$i<=count($myar[TITLE]);$i++) {

// Here, we want to read the TITLE, DESCRIPTION, and LINK of the RSS feed.
// You can read the values of any tag like this.
// $myar[TITLE][$i], $myar[SOMETHING][$i], $myar[SOMETAG][$i].... and so on..
// Here it goes

$title=$myar[TITLE][$i];
$text=$myar[DESCRIPTION][$i];
$link=$myar[LINK][$i];

if($title) {
echo <<<EOF
<a href="$link"><font size="2" face="Verdana" color="blue">$title</font></a><br>
<font size="2" face="Verdana">$text</font><br><br>
<hr width="50%" size="1" align="left">
EOF;
}

}



//################################################## ###################

// This is the function. It returns the array of the parsed XML data

function getXmlData($xml_doc) {

$n=0; // Counter used for arraying the XML data
$ar=array(); // The main array for storing parsed xml using xml_parse_into_struct()

// Parse the XML document
$parser = xml_parser_create();
xml_parse_into_struct($parser,$xml_doc,$vals,$inde x) or die(xml_error_string(xml_get_error_code($parser))) ;
xml_parser_free($parser);


$ttags=array(); // Temporary arry for storing tag names


// The main part. This is MY CREATION
// and this piece of code makes this script simple :)
// This is the "MAGIC LOOP" !! :)

for($n=0;$n<=count($vals)-1;$n++) {
if(trim($vals[$n][value])) {
$ar[$vals[$n][tag]][count($ar[$vals[$n][tag]])]=$vals[$n][value];
$ttags[$vals[$n][tag]]=$vals[$n][tag];
}
}


// Array for storing all the tag names
// This array will hold all the Tag names found in the XML document
// eg: ("TITLE","LINK","AUTHOR","DOMAIN")..
// Use this if you need it.

$tags=array();

// Extract and save the tag names to the array
foreach($ttags as $tagi) { array_push($tags,$tagi); }

return $ar;

}

?>

I changed line 37 so that it has the URL of my feed.

The script is currently uploaded here:

http://cardshark.us/scripts/rss-reader.php

Thank you for your time...

Chris
10-05-2009, 04:23 PM
This is the script I use... I think...

http://magpierss.sourceforge.net/

Modded of course. And obviously I could do better with special character encoding. Whatever. I actually don't 100% recall where I got it, But I think it is Magpie.

Here is the code



<?php

/*##############################################

RSS parser written for bMachine feed.
This script can be used to read ANY kind of RSS feeds from anywhere!


*--------------------------------------------*
Written by Kailash Nadh,
http://bnsoft.net , kailash@bnsoft.net

Author of bMachine, http://boastology.com

I wrote this script on the 1st day I studied
XML by running through the documentation at www.php.net ;)
*--------------------------------------------*

WARNING!: This script is Heavily commented! :)


This is how the script works.
> Reads the XML file
> Parses it into an array using xml_parse_into_struct()
> Converts that array into a more sensible, easily usable array
[ See the structure.txt file to get an idea of the array structure ]
> Finally, Display the data in anyway you want!

This script demonstrates the use of simple logic
to do Complex XML/RSS parsing functions
This script can be easily developed into a powerful application.

##############################################*/

global $data;


// Enter the url to an RSS feed
$url="http://www.websitepublisher.net/blog/feed/";

//$file = $url;
//$ch = curl_init($file);
//$fp = @fopen("temp.xml", "w");
//curl_setopt($ch, CURLOPT_FILE, $fp);
//curl_setopt($ch, CURLOPT_HEADER, 0);
//curl_exec($ch);
//curl_close($ch);
//fclose($fp);
//$file = "temp.xml";
//$fp = fopen($file, "r");
//$data = fread($fp, 1000);




$data=@fread(fopen("http://www.websitepublisher.net/blog/feed/","r"),10000) or die("Cant open $url!");

// Get the file contents

$myar=getXmlData($data);
// What ever it may be, the argument to this
// function should be the XML document's content


// Now $myar holds the fully parsed XML contents.
// It may have a number of tags, like <TITLE> , <LINK> , <AUTHOR> , <SOMETHING> ... and so on
// With a simple loop, you can extract all the tags and their values neatly

// Remember, $i<= should be lesser than or equal to the number of values
// a tag has. If you give count($myar), it will only take the number of tags.
// See structure.txt for better understanding.


for($i=0;$i<=count($myar[TITLE]);$i++) {

// Here, we want to read the TITLE, DESCRIPTION, and LINK of the RSS feed.
// You can read the values of any tag like this.
// $myar[TITLE][$i], $myar[SOMETHING][$i], $myar[SOMETAG][$i].... and so on..
// Here it goes

$title=$myar[TITLE][$i+1];
$link=$myar[LINK][$i+1];

if($title) {
if ($i>=0 and $i<6) {
echo "<li><a href=\"".$link."\">".$title."</a></li>";
}
}

}



//################################################## ###################

// This is the function. It returns the array of the parsed XML data

function getXmlData($xml_doc) {

//echo "xml_doc is ";
//echo $xml_doc;

$n=0; // Counter used for arraying the XML data
$ar=array(); // The main array for storing parsed xml using xml_parse_into_struct()

// Parse the XML document

$parser = xml_parser_create();
//$simple = "<para><note>simple note</note></para>";
$simple=$xml_doc;
//echo $simple;

xml_parse_into_struct($parser,$simple,$vals,$index );
// or die(xml_error_string(xml_get_error_code($parser))) ;
xml_parser_free($parser);


$ttags=array(); // Temporary arry for storing tag names


// The main part. This is MY CREATION
// and this piece of code makes this script simple :)
// This is the "MAGIC LOOP" !! :)

for($n=0;$n<=count($vals)-1;$n++) {
if(trim($vals[$n][value])) {
$ar[$vals[$n][tag]][count($ar[$vals[$n][tag]])]=$vals[$n][value];
$ttags[$vals[$n][tag]]=$vals[$n][tag];
}
}


// Array for storing all the tag names
// This array will hold all the Tag names found in the XML document
// eg: ("TITLE","LINK","AUTHOR","DOMAIN")..
// Use this if you need it.

$tags=array();

// Extract and save the tag names to the array
foreach($ttags as $tagi) { array_push($tags,$tagi); }

return $ar;

}

?>

Chris
10-05-2009, 04:24 PM
...haha, you have the same script.

So, that is exactly what I use on this site, and it works. If you change the URLs you should be fine. Or maybe your webserver has an odd setting that is breaking it.

Selkirk
10-05-2009, 07:37 PM
That script is way bad PHP.

The error message that its giving you is that your XML is bad. My guess is that your feed is too big for the simple fread function the way its used in that script. Hence, "unclosed tokens" because its only reading the first part of the feed.

You really should use SimplePie.

If thats the only bug in that script, I'd be surprised.

Mr. Pink
10-05-2009, 07:48 PM
Thanks for your reply, Chris.

I guess I made some progress...

I copied the script that Chris posted and changed two URLs. Now the script looks like this:


<?php

/*##############################################

RSS parser written for bMachine feed.
This script can be used to read ANY kind of RSS feeds from anywhere!


*--------------------------------------------*
Written by Kailash Nadh,
http://bnsoft.net , kailash@bnsoft.net

Author of bMachine, http://boastology.com

I wrote this script on the 1st day I studied
XML by running through the documentation at www.php.net ;)
*--------------------------------------------*

WARNING!: This script is Heavily commented! :)


This is how the script works.
> Reads the XML file
> Parses it into an array using xml_parse_into_struct()
> Converts that array into a more sensible, easily usable array
[ See the structure.txt file to get an idea of the array structure ]
> Finally, Display the data in anyway you want!

This script demonstrates the use of simple logic
to do Complex XML/RSS parsing functions
This script can be easily developed into a powerful application.

##############################################*/

global $data;


// Enter the url to an RSS feed
$url="http://feeds.feedburner.com/cardshark/";

//$file = $url;
//$ch = curl_init($file);
//$fp = @fopen("temp.xml", "w");
//curl_setopt($ch, CURLOPT_FILE, $fp);
//curl_setopt($ch, CURLOPT_HEADER, 0);
//curl_exec($ch);
//curl_close($ch);
//fclose($fp);
//$file = "temp.xml";
//$fp = fopen($file, "r");
//$data = fread($fp, 1000);




$data=@fread(fopen("http://feeds.feedburner.com/cardshark/","r"),10000) or die("Cant open $url!");

// Get the file contents

$myar=getXmlData($data);
// What ever it may be, the argument to this
// function should be the XML document's content


// Now $myar holds the fully parsed XML contents.
// It may have a number of tags, like <TITLE> , <LINK> , <AUTHOR> , <SOMETHING> ... and so on
// With a simple loop, you can extract all the tags and their values neatly

// Remember, $i<= should be lesser than or equal to the number of values
// a tag has. If you give count($myar), it will only take the number of tags.
// See structure.txt for better understanding.


for($i=0;$i<=count($myar[TITLE]);$i++) {

// Here, we want to read the TITLE, DESCRIPTION, and LINK of the RSS feed.
// You can read the values of any tag like this.
// $myar[TITLE][$i], $myar[SOMETHING][$i], $myar[SOMETAG][$i].... and so on..
// Here it goes

$title=$myar[TITLE][$i+1];
$link=$myar[LINK][$i+1];

if($title) {
if ($i>=0 and $i<6) {
echo "<li><a href=\"".$link."\">".$title."</a></li>";
}
}

}



//################################################## ###################

// This is the function. It returns the array of the parsed XML data

function getXmlData($xml_doc) {

//echo "xml_doc is ";
//echo $xml_doc;

$n=0; // Counter used for arraying the XML data
$ar=array(); // The main array for storing parsed xml using xml_parse_into_struct()

// Parse the XML document

$parser = xml_parser_create();
//$simple = "<para><note>simple note</note></para>";
$simple=$xml_doc;
//echo $simple;

xml_parse_into_struct($parser,$simple,$vals,$index );
// or die(xml_error_string(xml_get_error_code($parser))) ;
xml_parser_free($parser);


$ttags=array(); // Temporary arry for storing tag names


// The main part. This is MY CREATION
// and this piece of code makes this script simple :)
// This is the "MAGIC LOOP" !! :)

for($n=0;$n<=count($vals)-1;$n++) {
if(trim($vals[$n][value])) {
$ar[$vals[$n][tag]][count($ar[$vals[$n][tag]])]=$vals[$n][value];
$ttags[$vals[$n][tag]]=$vals[$n][tag];
}
}


// Array for storing all the tag names
// This array will hold all the Tag names found in the XML document
// eg: ("TITLE","LINK","AUTHOR","DOMAIN")..
// Use this if you need it.

$tags=array();

// Extract and save the tag names to the array
foreach($ttags as $tagi) { array_push($tags,$tagi); }

return $ar;

}

?>

I used another name and uploaded it here:

http://cardshark.us/scripts/rss_blog.php

Now at least I do get a result, but the script only returns my site's title linked to my site's URL (not the blog URL).

My feed URL is this:

http://feeds.feedburner.com/cardshark

I guess this is progress but now I'm kind of stuck again (I did try a few different things, though).

Mr. Pink
10-05-2009, 07:56 PM
Selkirk,

I just visited the SimplePie page again.

My problem is that I am a bit behind on all the terminology and can't figure out where to start looking for a solution on that page.

All I want to do is have 5 last titles on my blog to show up somewhere on my existing home page, which is a static HTML page. I think that I should just be able to add one tag to my HTML and a PHP script. But I just can't figure this out.

I don't know how much work it is at your end, but if you could just post a script I can copy and paste that would be great.

Blue Cat Buxton
10-06-2009, 03:31 AM
I tried your url in my version of the script - same one Chris posted - and got similar results to you, so we know it can work. What you need to do is select the headings that the script is pulling from the feed.

that is this bit here (from Chris's script as that is working for you:




$title=$myar[TITLE][$i+1];
$link=$myar[LINK][$i+1];



This is selecting the title and the link, which is exactly what you are getting.

try including:



$description=$myar[DESCRIPTION][$i+1];


and then add below the echo statement underneath:



echo $description;



If you look at the source of the feed, ie direct at http://feeds.feedburner.com/cardshark you should see the other fields you can get from the feed.

Mr. Pink
10-06-2009, 08:24 AM
Thank you, Blue Cat...

I did as you instructed and also changed the URL of the feed form this http://feeds.feedburner.com/cardshark to this http://cardshark-online.blogspot.com/feeds/posts/default

I also renamed the script and it is now here http://cardshark.us/scripts/rss_atom.php

I also tried another feed URL, this one http://cardshark-online.blogspot.com/atom.xml and it worked the same as http://cardshark-online.blogspot.com/feeds/posts/default

Now the script looks like this:


<?php

/*##############################################

RSS parser written for bMachine feed.
This script can be used to read ANY kind of RSS feeds from anywhere!


*--------------------------------------------*
Written by Kailash Nadh,
http://bnsoft.net , kailash@bnsoft.net

Author of bMachine, http://boastology.com

I wrote this script on the 1st day I studied
XML by running through the documentation at www.php.net ;)
*--------------------------------------------*

WARNING!: This script is Heavily commented! :)


This is how the script works.
> Reads the XML file
> Parses it into an array using xml_parse_into_struct()
> Converts that array into a more sensible, easily usable array
[ See the structure.txt file to get an idea of the array structure ]
> Finally, Display the data in anyway you want!

This script demonstrates the use of simple logic
to do Complex XML/RSS parsing functions
This script can be easily developed into a powerful application.

##############################################*/

global $data;


// Enter the url to an RSS feed
$url="http://cardshark-online.blogspot.com/feeds/posts/default";

//$file = $url;
//$ch = curl_init($file);
//$fp = @fopen("temp.xml", "w");
//curl_setopt($ch, CURLOPT_FILE, $fp);
//curl_setopt($ch, CURLOPT_HEADER, 0);
//curl_exec($ch);
//curl_close($ch);
//fclose($fp);
//$file = "temp.xml";
//$fp = fopen($file, "r");
//$data = fread($fp, 1000);




$data=@fread(fopen("http://cardshark-online.blogspot.com/feeds/posts/default","r"),10000) or die("Cant open $url!");

// Get the file contents

$myar=getXmlData($data);
// What ever it may be, the argument to this
// function should be the XML document's content


// Now $myar holds the fully parsed XML contents.
// It may have a number of tags, like <TITLE> , <LINK> , <AUTHOR> , <SOMETHING> ... and so on
// With a simple loop, you can extract all the tags and their values neatly

// Remember, $i<= should be lesser than or equal to the number of values
// a tag has. If you give count($myar), it will only take the number of tags.
// See structure.txt for better understanding.


for($i=0;$i<=count($myar[TITLE]);$i++) {

// Here, we want to read the TITLE, DESCRIPTION, and LINK of the RSS feed.
// You can read the values of any tag like this.
// $myar[TITLE][$i], $myar[SOMETHING][$i], $myar[SOMETAG][$i].... and so on..
// Here it goes

$title=$myar[TITLE][$i+1];
$link=$myar[LINK][$i+1];
$description=$myar[DESCRIPTION][$i+1];
echo $description;

if($title) {
if ($i>=0 and $i<6) {
echo "<li><a href=\"".$link."\">".$title."</a></li>";
}
}

}



//################################################## ###################

// This is the function. It returns the array of the parsed XML data

function getXmlData($xml_doc) {

//echo "xml_doc is ";
//echo $xml_doc;

$n=0; // Counter used for arraying the XML data
$ar=array(); // The main array for storing parsed xml using xml_parse_into_struct()

// Parse the XML document

$parser = xml_parser_create();
//$simple = "<para><note>simple note</note></para>";
$simple=$xml_doc;
//echo $simple;

xml_parse_into_struct($parser,$simple,$vals,$index );
// or die(xml_error_string(xml_get_error_code($parser))) ;
xml_parser_free($parser);


$ttags=array(); // Temporary arry for storing tag names


// The main part. This is MY CREATION
// and this piece of code makes this script simple :)
// This is the "MAGIC LOOP" !! :)

for($n=0;$n<=count($vals)-1;$n++) {
if(trim($vals[$n][value])) {
$ar[$vals[$n][tag]][count($ar[$vals[$n][tag]])]=$vals[$n][value];
$ttags[$vals[$n][tag]]=$vals[$n][tag];
}
}


// Array for storing all the tag names
// This array will hold all the Tag names found in the XML document
// eg: ("TITLE","LINK","AUTHOR","DOMAIN")..
// Use this if you need it.

$tags=array();

// Extract and save the tag names to the array
foreach($ttags as $tagi) { array_push($tags,$tagi); }

return $ar;

}

?>

When I go to http://cardshark.us/scripts/rss_atom.php it returns the title of the last blog post linked back to the URL of the script, i.e. http://cardshark.us/scripts/rss_atom.php

I don't think that's what this script is supposed to do, right? Shouldn't the title of the blog post be linked to the URL of that blog post? And shouldn't there be a short description under the title?

How can I be getting such different results, after I basically just copied and pasted the scrip you guys are using?

Any ideas...

Selkirk
10-06-2009, 09:11 AM
Mr. Pink, There are a lot of tutorials (http://simplepie.org/wiki/tutorial/start) for SimplePie. SimplePie is bundled with WordPress, BTW. this one (http://css-tricks.com/video-screencasts/55-adding-rss-content-with-simplepie/) (video) looks like a good place to start.

Mr. Pink
10-06-2009, 09:46 PM
Selkirk,

The tutorial you pointed me to was really helpful. So, let me start by saying, thank you.

I was actually able to build a script (my first ever) and it works. Here is the URL of the script:

http://cardshark.us/scripts/rss_1.php

...you can see what it returns exactly what I wanted it to. So, it works.



Now, this brings me to the next question. I actually want to display this on a specific part of my home page, which is a static HTML page, with a .html extension.

Is it at all possible to do this? I would imagine that I could just put some short tag on my .html page that would display the text that the .php script is returning. Or is that not possible?

Selkirk
10-07-2009, 08:37 AM
I'm glad that worked for you, Mr. Pink.

if your page is an index.html then its pretty easy to just rename it index.php and insert your feed code.

If your page is something else foo.html and you don't want to just rename it to foo.php, then you can create a .htaccess file in the same directory with the following:


<files foo.html>
AddHandler x-httpd-php5 .html
</files>

This forces the php interpreter to process the specified file as a php page regardless of file extension. I haven't tried this in a long time, YMMV.

Mr. Pink
10-07-2009, 05:30 PM
Thanks again, Selkirk,

But I looked it up online and slightly changed the code.


<files a.html>
AddType application/x-httpd-php .html
</files>


This makes my file a.html interpret PHP.

So, I guess it works.

thanks...