无法从passthru得到答案

时间:2016-03-04 10:34:09

标签: javascript php passthru

这是一个简单的PHP代码

<!DOCTYPE html>
<html>
<head>
<title>Test PDF</test>
<style>
table{width:100%; border-collapse:collapse; table-layout:auto; vertical-align:top; margin-bottom:15px; border:1px solid #CCCCCC;}
table thead th{font-size: 2px; color:#FFFFFF; background-color:#666666; border:1px solid #CCCCCC; border-collapse:collapse; text-align:center; table-layout:auto; vertical-align:middle;}
table tbody td{vertical-align:top; border-collapse:collapse; border-left:1px solid #CCCCCC; border-right:1px solid #CCCCCC; font-size: 2px;}
table thead th, table tbody td{padding:5px; border-collapse:collapse;}
table tbody tr.light{color:#979797; background-color:#F7F7F7;}
table tbody tr.dark{color:#979797; background-color:#E8E8E8;}
</style>
</head>
<body>
<html>
<?php
require_once "config.php";

//Start html2pdpf session
ob_start();
//Get tablename
$tablename=$_POST["tablename"];
$ShowPivotTableResult='SELECT * FROM '.$tablename.'';
    $resultLeast=mysqli_query($conn,$ShowPivotTableResult) or die(mysqli_error($conn));
if (!$resultLeast) {
    die("Query to show fields from table failed");
    }
//Display Pivot Table content - script
$fields_num = mysqli_num_fields($resultLeast);
    echo "<table border='1px' CELLSPACING='0' cellpadding='2'><thead><tr>";
    // printing table headers
for($i=0; $i<$fields_num; $i++)
{
    $field = mysqli_fetch_fields($resultLeast);
    echo '<th>'.$field[$i]->name.'</th>';
}
echo "</tr></thead><tbody>\n";
// printing table rows
while($rowLEAST = mysqli_fetch_row($resultLeast))
{
    echo "<tr>";
    // $row is array... foreach( .. ) puts every element
    // of $row to $cell variable
    foreach($rowLEAST as $cellPIVOT)
    echo "<td>$cellPIVOT</td>";
    echo "</tr>\n";
    }
    echo '</tbody></table><br/><br/>';

$content = ob_get_clean();

    // convert
    require_once('html2pdf.class.php');
    try
    {
        $html2pdf = new HTML2PDF('L', 'A4', 'fr', true, 'UTF-8', 0);
        $html2pdf->pdf->SetDisplayMode('fullpage');
        $html2pdf->writeHTML($content, isset($_GET['vuehtml']));
        ob_end_clean();
        $html2pdf->Output(''.$tablename.'.pdf');
    }
    catch(HTML2PDF_exception $e) {
        echo $e;
        exit;
    }

mysqli_close($conn);
?>
</body>
</html>

node.js脚本只有一个带有对象的变量结果。

<?php
ob_start(passthru('/usr/bin/env node getResult.js '));
$data = ob_get_contents();
ob_end_clean();
//  console shows Data as String but $data is still empty
    echo "Data : " . $data;
?>

问题:我需要getResult.js脚本中的变量,但我不能在php中捕获它。任何想法?

2 个答案:

答案 0 :(得分:1)

为什么要麻烦输出缓冲区呢?

exec( '/usr/bin/env node getResult.js', $data );
echo "Data: " . $data;

exec()将命令的完整输出写入提供的变量。

答案 1 :(得分:0)

这是你需要的吗?

<?php
ob_start(); //note, no parameter here
passthru('/usr/bin/env node getResult.js '); // this should be captured
$data = ob_get_contents();
ob_end_clean();

echo "Data : " . $data;
?>