View Full Version : php dilemma for rss feed

04-28-2010, 05:19 AM
Hello all, I'm using this code below...

$cache_time = 3600*24; // 24 hours

$cache_file = $_SERVER['DOCUMENT_ROOT'].'/cache/test.rss';
$timedif = @(time() - filemtime($cache_file));

if (file_exists($cache_file) && $timedif < $cache_time) {
$string = file_get_contents($cache_file);
} else {
$string = file_get_contents('http://www.web-development-blog.com/feed/');
if ($f = @fopen($cache_file, 'w')) {
fwrite ($f, $string, strlen($string));
$xml = simplexml_load_string($string);

// place the code below somewhere in your html
echo '
<div id="rssbox">
$count = 0;
$max = 3;
// the next object is an example for a feed from wordpress
foreach ($xml->channel->item as $val) {
if ($count < $max) {
echo '
<strong>'.$val->title.'</strong><br />
'.$val->description.' | <a href="'.$val->link.'">More &gt;</a>
echo '

I'm using it to push a blog to my site, but i cant seem to get it to work. I'll type out now exactly what I'm doing so you can point out where I'm going wrong. This is the first time I've played with php so I dont know if what I'm doing is right or wrong.

1. I open my html dreamweaver document where I want to show this blog feed
2. I place the above code in where I want it to appear on the page
3. I edit the '$string = file_get_contents' to my blog address
4. I select save as and change the .html extension to .php
5. I preview the page in my browser and there is no blog there and the bottom third of the code literally shows up on the page preview

Where am I going wrong?

Thanks in advance for any help.

04-28-2010, 05:40 AM
It's very hard to diagnose what is wrong without being able to test it. Do you get any error messages?

What do you mean by this?

the bottom third of the code literally shows up on the page preview

The simplest thing to check first: did you switch to code view rather than design view? If you paste the code into the design view it will just display the text. If you paste it in code view it'll actually execute it as PHP.

04-28-2010, 05:48 AM
Hi there, what I mean by that is, on the design view of the page, not the code view. Some of that php code shows up. I am doing everything in code view yes. Is it ok to have a .html file then just save it as .php? Ps, is that all I need to change in the php code, the '$string = file_get_contents('http://www.web-development-blog.com/feed/');'?

04-28-2010, 06:11 AM
Ok, then that's not the problem. Next...

.php is processed as PHP. This means that anything between <?php and ?> is run as PHP then outputs HTML and the browser sees pure HTML. Renaming a .htm page to .php without any <?php ?> tags will be fine. If you add some, then that will additionally be processed as PHP-- only in .php, not .htm-- so this is required.

I don't fully understand what this code is supposed to do. Remember, code is just the use of many different tools to create a "working" whole. It can be written in many ways and do many things. Every script is different, so I'm not quite sure about this one yet. I see the basic steps, though, and one part jumps out as unusual.
At the end there are a lot of symbols like $something->$something.
Without getting into too much of the complex details here, this is something used in "classes" and you don't have any classes defined in your script.
It also suggests that this is to be used with wordpress. Is this supposed to be connected to wordpress and it is not? Is this meant to run alone?

Perhaps a link to where you got this would help explain what it's supposed to do (and then why it's not doing it).

04-28-2010, 06:58 AM
this is the link where I got it from http://www.finalwebsites.com/snippets.php?id=49

So are you saying that since my file is .php i dont need my <?php and ?> ??

04-28-2010, 07:17 AM
<?php ?> marks PHP code. You DO need it if you are using PHP code and not just plain HTML. It only works in .php pages, not .htm.
So, yes, you're doing this correctly.

But the main point of what I said is that it looks like that code uses things that aren't already included-- it needs a base of wordpress.
I suppose it's based on the xml function, but I'm not sure how this relates to your blog.

I'd suggest starting on some basic PHP tutorials to get an idea of how all this works together then returning to this to figure out how to complete it.