2013-10-01 61 views
0

我正在構建控制檯應用程序,並希望使用已編寫的代碼而不進行修改。但是,我需要一個命令的輸出。是否有可能這樣做:PHP:是否有可能獲得非阻塞輸出緩衝區?

function func() { 
    /* Let's say this function contains already written 
     code and it would be easier to rewrite it than modify */ 
    echo 'Confirm action'; 
    $input = fgets(fopen('php://stdin', 'r')); 
    if ($input == 'y') echo 'okay'; 
    else echo 'no'; 
    return 0; 
} 

$code = func(); 
// func() returns non zero only on error and confirming/declining an action returns 0. 
// I want to know if the action was confirmed. 
// Using ob_start() prevents echo from working in the function, 
// i.e. the user sees a blank screen waiting for input. 

這是可能的嗎?

我正在寫這與Yii框架。任何想法讚賞。

回答

0

與POPEN解決了這個,例如:

$handle = popen('php yiic migrate create '.$name, 'r'); 
    $output = ''; 
    while (!feof($handle)) 
    { 
     $read = fread($handle, 2096); 
     $output .= $read; 
     echo $read; 
    } 
    $exitCode = pclose($handle);