By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,885 Members | 1,499 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,885 IT Pros & Developers. It's quick & easy.

evaluate code before serving it

P: n/a
Situation: I store news articles as individual PHP files. Each file
contains HTML and now and then some embedded PHP snippets.

Serving those news articles on the Web works fine, through include().
But I want to also serve them through RSS, and found that in that case
the PHP source code is served -- it's not being evaluated.

So it seems obvious that I need to first evaluate those PHP snippets,
store the result in a variable, and use *that* as the input for the RSS
generator script. I thought that'd be pretty straight-forward, but for
the life of me I can't figure out how to do it.

I've experimented with eval() and ob_start() but either I misunderstand
how to use them, or they're not the right tools for this use.

<http://de.php.net/manual/en/function.eval.php#76339claims a solution,
but it doesn't work for me. (I get no output whatsover using that
approach.)
Any suggestions? TIA!

--
Sander Tekelenburg, <http://www.euronet.nl/~tekelenb/>

Mac user: "Macs only have 40 viruses, tops!"
PC user: "SEE! Not even the virus writers support Macs!"
Jul 24 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Rik
On Tue, 24 Jul 2007 19:59:27 +0200, Sander Tekelenburg
<us**@domain.invalidwrote:
Situation: I store news articles as individual PHP files. Each file
contains HTML and now and then some embedded PHP snippets.

Serving those news articles on the Web works fine, through include().
But I want to also serve them through RSS, and found that in that case
the PHP source code is served -- it's not being evaluated.

So it seems obvious that I need to first evaluate those PHP snippets,
store the result in a variable, and use *that* as the input for the RSS
generator script. I thought that'd be pretty straight-forward, but for
the life of me I can't figure out how to do it.
Depends on how you generator script works offcourse... Normally is
shouldn't be a problem.
I've experimented with eval() and ob_start() but either I misunderstand
how to use them, or they're not the right tools for this use.

<http://de.php.net/manual/en/function.eval.php#76339claims a solution,
but it doesn't work for me. (I get no output whatsover using that
approach.)
Simple eval solution:
The manual says: "To mix HTML output and PHP code you can use a closing
PHP tag to leave PHP mode."

ob_start();
eval('?>'.file_get_contents('file.php').'<?php');
$string = ob_get_clean();
Then again, that would basically be the same as:
ob_start();
include('file.php');
$string = ob_get_clean();

Which is preferable.

It could be that your RSS-generator only swallows files/urls, and not
strings? In that case:

//get a unique filename in temporary dir
$file = tempnam();
//open file
$fh = fopen($file,'w');
//start buffer
ob_start();
//run file
include('file.php');
//write output to temporary file
fwrite($fh,ob_get_clean());
//...And serve the filename $file to your script.
//remove temporary file
unlink($file);

You might get into trouble when the script sets headers though...

More simple solution: if the RSS generator gets urls, just give him the
exact url of the file holding the article.

As you can see, without knowing anything about your RSS generator we're
flying blind here.
--
Rik Wasmus
Jul 24 '07 #2

P: n/a
Sander Tekelenburg wrote:
So it seems obvious that I need to first evaluate those PHP snippets,
store the result in a variable, and use *that* as the input for the RSS
generator script.
Post your RSS generator script.

--
Toby A Inkster BSc (Hons) ARCS
[Geek of HTML/SQL/Perl/PHP/Python/Apache/Linux]
[OS: Linux 2.6.12-12mdksmp, up 33 days, 21:55.]

Parsing an HTML Table with PEAR's XML_HTTPSax3
http://tobyinkster.co.uk/blog/2007/0...table-parsing/
Jul 24 '07 #3

P: n/a
In article <op.tvzecvwtqnv3q9@metallium>,
Rik <lu************@hotmail.comwrote:
On Tue, 24 Jul 2007 19:59:27 +0200, Sander Tekelenburg
<us**@domain.invalidwrote:
[...] Each file
contains HTML and now and then some embedded PHP snippets.

[...] I need to first evaluate those PHP snippets,
store the result in a variable, and use *that* as the input for the RSS
generator script.
[...]
ob_start();
eval('?>'.file_get_contents('file.php').'<?php');
$string = ob_get_clean();
Yeah, I had tried that. But for some reason this results in $string
being (defined but) empty.
Then again, that would basically be the same as:
ob_start();
include('file.php');
$string = ob_get_clean();
Bingo! That works. Thanks!

[...]
It could be that your RSS-generator only swallows files/urls, and not
strings?
It eats strings. I'm making use of FeedCreator.class.php. See
<http://www.bitfolge.de/rsscreator-en.html>

So the code essentially is this:

require_once("feedcreator.class.php");
$news = new UniversalFeedCreator();
//loop through list of news files
ob_start();
include($s_pathtofile);
$s_content = ob_get_clean();
$item = new FeedItem();
$item->description = $s_content;
$item->addItem($item);
//end loop
$news->saveFeed("RSS1.0", "path/file.xml", false);
In that case:
[...]
//write output to temporary file [...]
Yeah, I was considering going there. Glad I don't have to :)

--
Sander Tekelenburg, <http://www.euronet.nl/~tekelenb/>

Mac user: "Macs only have 40 viruses, tops!"
PC user: "SEE! Not even the virus writers support Macs!"
Jul 24 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.