[英]How to get output from local script in PHP?
For example, in foo.php: 例如,在foo.php中:
<?php echo 'hello'; ?>
And in bar.php, I want to get the output of foo.php (which is hello) and do some formatting before outputting to browser. 在bar.php中,我想得到foo.php的输出(这是hello)并在输出到浏览器之前进行一些格式化。 Is there any way to do this?
有没有办法做到这一点?
Or even further, if the webserver can run both PHP and Python scripts, can a PHP script get the output of a Python script? 或者更进一步,如果Web服务器可以运行PHP和Python脚本,PHP脚本是否可以获得Python脚本的输出?
Edit: PHP function file_get_contents() can do this only for remote scripts. 编辑:PHP函数file_get_contents()只能为远程脚本执行此操作。 If it is used on local scripts, it will return the contents of the whole script.
如果它在本地脚本上使用,它将返回整个脚本的内容。 In the example above, it returns rather than hello.
在上面的例子中,它返回而不是hello。 And I don't want to use exec()/system() things and CGI.
而且我不想使用exec()/ system()和CGI。
You can use PHP's output buffers and functions like ob_start(); 您可以使用PHP的输出缓冲区和函数,如ob_start(); and ob_get_contents();
和ob_get_contents(); to read the contents of your PHP output into a string.
将PHP输出的内容读入字符串。
Here is the example on php.net: 这是php.net上的示例:
<?php
function callback($buffer)
{
// replace all the apples with oranges
return (str_replace("apples", "oranges", $buffer));
}
ob_start("callback");
?>
<html>
<body>
<p>It's like comparing apples to oranges.</p>
</body>
</html>
<?php
ob_end_flush();
?>
And another: 而另一个:
<?php
ob_start();
echo "Hello ";
$out1 = ob_get_contents();
echo "World";
$out2 = ob_get_contents();
ob_end_clean();
var_dump($out1, $out2);
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.