Capturing data from another php file

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

    #1

    Capturing data from another php file

    I'm trying to capture data that is created by launching a php file,
    for example the last 10 posts in a message forum. If last10.php is
    called directly in your browser you'll see hyperlinks to the last 10
    posts. This file is located in /home/mydomain/last10.php .

    Now I'm trying to capture that data in a template in a CMS I'm using
    where you can launch php scripts. I'm just unsure how one can "run"
    last10.php and capture the stream into a variable and then include
    that variable to be outputted in another template. As long as I
    capture the stream, that variable can simply be placed in another
    template so I'm not so concerned about that.

    Now I'm familiar with includes and requires, but this doesn't really
    "launch" the file, it only includes the items so that you can use the
    variables from my understanding. The problem is that I need the whole
    stream from the last10.php to be captured into one variable that will
    then print out this html that was output.

    Learning this stuff is truly amazing... I hope to quit my day job. :)
    Thanks... Mike

  • NC

    #2
    Re: Capturing data from another php file

    Mike wrote:[color=blue]
    >
    > I'm trying to capture data that is created by launching
    > a php file, for example the last 10 posts in a message
    > forum. If last10.php is called directly in your browser
    > you'll see hyperlinks to the last 10 posts. This file
    > is located in /home/mydomain/last10.php .[/color]

    There are at least two ways of doing it.

    1. Use output buffering. Additionally, you might want
    to wrap the include() call into a function to prevent
    accidental modification of variables in the calling
    script by last10.php:

    function get_last_10() {
    ob_start();
    include('last10 .php');
    return ob_get_clean();
    }

    echo get_last_10(); // output last 10 posts
    $last10 = get_last_10(); // capture the posts into a variable

    2. Capture the output of last10.php using file system
    functions and the HTTP wrapper. To simply output
    the last 10 posts you could do:

    readfile('http://localhost/last10.php');

    To capture the last 10 posts into a variable, you
    can do:

    $last10 = file_get_conten ts('http://localhost/last10.php');

    Cheers,
    NC

    Comment

    • Mike

      #3
      Re: Capturing data from another php file

      On 19 Mar 2005 02:01:14 -0800, "NC" <nc@iname.com > wrote:
      [color=blue]
      >Mike wrote:[color=green]
      >>
      >> I'm trying to capture data that is created by launching
      >> a php file, for example the last 10 posts in a message
      >> forum. If last10.php is called directly in your browser
      >> you'll see hyperlinks to the last 10 posts. This file
      >> is located in /home/mydomain/last10.php .[/color]
      >
      >There are at least two ways of doing it.
      >
      >1. Use output buffering. Additionally, you might want
      > to wrap the include() call into a function to prevent
      > accidental modification of variables in the calling
      > script by last10.php:
      >
      > function get_last_10() {
      > ob_start();
      > include('last10 .php');
      > return ob_get_clean();
      > }
      >
      > echo get_last_10(); // output last 10 posts
      > $last10 = get_last_10(); // capture the posts into a variable
      >
      >2. Capture the output of last10.php using file system
      > functions and the HTTP wrapper. To simply output
      > the last 10 posts you could do:
      >
      > readfile('http://localhost/last10.php');
      >
      > To capture the last 10 posts into a variable, you
      > can do:
      >
      > $last10 = file_get_conten ts('http://localhost/last10.php');
      >
      >Cheers,
      >NC[/color]

      SUPERB!!!!! I tried like crazy to use the output buffering method you
      mentioned. For some reason include produced nothing (blank) and trying
      to use require produced a completely blank page and I'm guessing it
      wouldn't load.

      However, using the readfile method was perfect! Thanks so much!!!
      That's one that I hadn't seen used. :)

      Mike

      Comment

      • NC

        #4
        Re: Capturing data from another php file

        Mike wrote:[color=blue]
        >
        > I tried like crazy to use the output buffering method you
        > mentioned. For some reason include produced nothing (blank)
        > and trying to use require produced a completely blank page
        > and I'm guessing it wouldn't load.[/color]

        Most likely, you didn't specify the correct file name/path.
        In case of an error, include() generates a warning, while
        require() halts the script...

        Cheers,
        NC

        Comment

        • CJ Llewellyn

          #5
          Re: Capturing data from another php file

          On Sat, 19 Mar 2005 03:00:08 -0500, Mike wrote:
          [color=blue]
          > I'm trying to capture data that is created by launching a php file,
          > for example the last 10 posts in a message forum. If last10.php is
          > called directly in your browser you'll see hyperlinks to the last 10
          > posts. This file is located in /home/mydomain/last10.php .
          >
          > Now I'm trying to capture that data in a template in a CMS I'm using
          > where you can launch php scripts. I'm just unsure how one can "run"
          > last10.php and capture the stream into a variable and then include
          > that variable to be outputted in another template. As long as I
          > capture the stream, that variable can simply be placed in another
          > template so I'm not so concerned about that.
          >
          > Now I'm familiar with includes and requires, but this doesn't really
          > "launch" the file, it only includes the items so that you can use the
          > variables from my understanding.[/color]

          Then your understanding needs changing.

          include and require allow different scripts to share the same program code.

          common.inc.php

          <?php

          function HelloWorld() {
          return "Hello World<br>\n";
          }

          ?>

          a.php

          <?php

          include('common .inc.php');

          echo HelloWorld();

          ?>

          b.php

          <?php

          include('common .inc.php');

          for($i=0;$i<10; $i++)
          {
          echo HelloWorld();
          }

          ?>

          You should not be looking at including last10.php into your program but
          the code that last10.php uses to generate it's output.

          It is likely to be calling a database script that returns a result set
          which can be far more easily used than the output from a web page.

          Comment

          • Chung Leong

            #6
            Re: Capturing data from another php file

            "Mike" <tkdREMOVESPAMi nthecity@yahSPA Moo.com> wrote in message
            news:r0go315jnm 96tee1c9a8krlm0 4g8m5uv1c@4ax.c om...[color=blue]
            > On 19 Mar 2005 02:01:14 -0800, "NC" <nc@iname.com > wrote:
            >[color=green]
            > >Mike wrote:[color=darkred]
            > >>
            > >> I'm trying to capture data that is created by launching
            > >> a php file, for example the last 10 posts in a message
            > >> forum. If last10.php is called directly in your browser
            > >> you'll see hyperlinks to the last 10 posts. This file
            > >> is located in /home/mydomain/last10.php .[/color]
            > >
            > >There are at least two ways of doing it.
            > >
            > >1. Use output buffering. Additionally, you might want
            > > to wrap the include() call into a function to prevent
            > > accidental modification of variables in the calling
            > > script by last10.php:
            > >
            > > function get_last_10() {
            > > ob_start();
            > > include('last10 .php');
            > > return ob_get_clean();
            > > }
            > >
            > > echo get_last_10(); // output last 10 posts
            > > $last10 = get_last_10(); // capture the posts into a variable
            > >
            > >2. Capture the output of last10.php using file system
            > > functions and the HTTP wrapper. To simply output
            > > the last 10 posts you could do:
            > >
            > > readfile('http://localhost/last10.php');
            > >
            > > To capture the last 10 posts into a variable, you
            > > can do:
            > >
            > > $last10 = file_get_conten ts('http://localhost/last10.php');
            > >
            > >Cheers,
            > >NC[/color]
            >
            > SUPERB!!!!! I tried like crazy to use the output buffering method you
            > mentioned. For some reason include produced nothing (blank) and trying
            > to use require produced a completely blank page and I'm guessing it
            > wouldn't load.[/color]

            Is the include() statement inside a function per NC's suggestion? If so, it
            could be a variable scoping issue. Say you have some config variables
            defined globally and functions that make use of them:

            $db_user = 'dingo';
            $db_pass = '4t3_my_b4by';

            function connect_to_db() {
            global $db_user, $db_pass
            // ...
            }

            When the include() happens inside a function, those variable will no longer
            be global, so functions that depend on them will cease to work.


            Comment

            Working...