Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8300679
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T16:39:00+00:00 2026-06-08T16:39:00+00:00

I need to capture the output of a child process in PHP to manipulate

  • 0

I need to capture the output of a child process in PHP to manipulate it.

I’ve two little scripts, parent.php and child.php

This is the code of parent.php

    $exe = $_SERVER['_'];
    $command = '/var/www/child.php';

    echo "\nSTART\n";

    $pid = pcntl_fork();

    ob_start();

    echo "\nHELLO\n";

    if ($pid == -1)
        exit("Could not fork");
    else if ($pid)
        pcntl_waitpid($pid, $status);
    else
    {
        $args = array
        (
            "arg1" => 'one',
            "arg2" => 'two',
            "arg3" => 'three'
        );

        pcntl_exec($exe, array($command), $args);
    }

    $content = ob_get_contents();
    ob_end_clean();

    echo "\nEND\n";

    echo $content . "\n";

And this is the code of child.php

    echo "\n\tHELLO FROM TEST\n";

    echo "\t"; var_dump($_SERVER['arg1']);
    echo "\t"; var_dump($_SERVER['arg2']);
    echo "\t"; var_dump($_SERVER['arg3']);

Now… this is my output:

START

    HELLO FROM TEST
    string(3) "one"
    string(3) "two"
    string(5) "three"

END

HELLO

But I need THIS output

START

END

HELLO

    HELLO FROM TEST
    string(3) "one"
    string(3) "two"
    string(5) "three"

Is this possible that the classic buffer workaround with ob_start() doesn’t work??

Note: is not important the order of output, i need to capture it for manipulation purposes

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T16:39:02+00:00Added an answer on June 8, 2026 at 4:39 pm

    I’ve found the solution.

    You must use the proc_open() function that allow to you to work directly with the stream buffer:

    echo "\nSTART\n";
    
    $stream = null;
    
    $descriptorspec = array(
       0 => array("pipe", "r"),
       1 => array("pipe", "w"),
       2 => array("file", "/tmp/error-output.txt", "a")
    );
    
    $cwd = '/tmp';
    $env = array('some_option' => 'aeiou');
    
    $process = proc_open('/usr/bin/php', $descriptorspec, $pipes, $cwd, $env);
    
    if (is_resource($process))
    {
        fwrite($pipes[0], '<?php echo "HELLO FROM TEST"; ?>'); // here directly the code of child.php
        fclose($pipes[0]);
    
        $stream = stream_get_contents($pipes[1]);
        fclose($pipes[1]);
    
        proc_close($process);
    }
    
    echo "\nEND\n";
    
    echo "\n$stream\n";
    

    Output is:

    START
    
    END
    
    HELLO FROM TEST
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello guys i need to capture the output of an external command, herefore I
$('input').change(function(){ alert($(this).val()); }); $('input').val(1); This dont work. I need capture the change input with
I have a need to capture all the rendered output/html of a view so
I need to capture such string as a TWO separate matches: MLB m L
Using Java is it possible to capture the speaker output? This output is not
I need to be able to capture the output of a controller action as
I'd like to capture the output of var_dump to a string. The PHP documentation
I have some existing code making use of System.Diagnostics.Process.Start to start a child process.
I want to capture output from a C program I'm launching like this: p
I need to capture the output and error of a command in my bash

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.