我想从PHP页面调用PHP页面,并将调用的页面的输出作为单个变量返回。
require不适合我的目的,因为我需要将输出存储为变量以供以后使用,而不是立即输出。
即:
page1.php
<?php
echo 'Hello World!<br>';
$output = call_page('page2.php');
echo 'Go for it!<br>';
echo $output;
?>
page2.php
<?php
echo "Is this real life?<br>";
?>将输出:
Hello World!
Go for it!
Is this real life?发布于 2011-09-20 03:30:47
require实际上正是您想要的(与输出缓冲相结合):
ob_start();
require 'page2.php';
$output = ob_get_clean();发布于 2014-06-25 15:11:57
要在使用file_get_contents()时澄清一些混淆,请注意:
将
$contents = file_get_contents('http://www.example-domain.com/example.php');
$contents = file_get_contents('./example.php');
发布于 2011-09-20 03:32:25
您可以使用file_get_contents方法,该方法返回一个包含所请求页面内容的字符串- http://php.net/manual/en/function.file-get-contents.php
$contents = file_get_contents('http://www.stackoverflow.com/');
echo $contents;https://stackoverflow.com/questions/7476216
复制相似问题