从另一个 PHP 文件读取回显输出

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/631388/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 23:19:20  来源:igfitidea点击:

Read echo'ed output from another PHP file

phpserver-sideecho

提问by rzlines

I want 1 PHP file to "run" (include?) another PHP file on the same server, and access its echo'ed output as a string.

我希望 1 个 PHP 文件在同一台服务器上“运行”(包括?)另一个 PHP 文件,并以字符串形式访问其回显输出。

How do i do this in PHP? Any inbuilt functions to do this?

我如何在 PHP 中执行此操作?任何内置功能可以做到这一点?

Or any better way of executing another PHP file and getting its output?

或者有什么更好的方法来执行另一个 PHP 文件并获取其输出?

回答by Jesse Rusak

You can use PHP's output bufferingto accomplish this:

您可以使用 PHP 的输出缓冲来完成此操作:

ob_start(); // begin collecting output

include 'myfile.php';

$result = ob_get_clean(); // retrieve output from myfile.php, stop buffering

$resultwill then contain the text.

$result然后将包含文本。

回答by Typewar

You can't include a PHP script that is on an external website/server into your local script - unless you enable allow_url_include on your php.ini (if you have access to it)

您不能将外部网站/服务器上的 PHP 脚本包含到您的本地脚本中 - 除非您在 php.ini 上启用 allow_url_include (如果您有权访问它)

Instead, you can let that website/server render the page and get the resulting HTML output on your local script by doing this:

相反,您可以通过执行以下操作让该网站/服务器呈现页面并在本地脚本上获取生成的 HTML 输出:

$result = file_get_contents('http://127.0.0.1/myfile.php');