evaluate code before serving it

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sander Tekelenburg

    #1

    evaluate code before serving it

    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.p hp#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!"
  • Rik

    #2
    Re: evaluate code before serving it

    On Tue, 24 Jul 2007 19:59:27 +0200, Sander Tekelenburg
    <user@domain.in validwrote:
    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.p hp#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('f ile.php').'<?ph p');
    $string = ob_get_clean();


    Then again, that would basically be the same as:
    ob_start();
    include('file.p hp');
    $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.p hp');
    //write output to temporary file
    fwrite($fh,ob_g et_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

    Comment

    • Toby A Inkster

      #3
      Re: evaluate code before serving it

      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

      Comment

      • Sander Tekelenburg

        #4
        Re: evaluate code before serving it

        In article <op.tvzecvwtqnv 3q9@metallium>,
        Rik <luiheidsgoeroe @hotmail.comwro te:
        On Tue, 24 Jul 2007 19:59:27 +0200, Sander Tekelenburg
        <user@domain.in validwrote:
        >
        [...] 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('f ile.php').'<?ph p');
        $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.p hp');
        $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.cla ss.php. See
        <http://www.bitfolge.de/rsscreator-en.html>

        So the code essentially is this:

        require_once("f eedcreator.clas s.php");
        $news = new UniversalFeedCr eator();
        //loop through list of news files
        ob_start();
        include($s_path tofile);
        $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!"

        Comment

        Working...