摘要
我正在尝试使用fread
(或readfile
)函数(PHP)来获得另一个PHP页面的输出结果。例如,我想读取(甚至作为另一个文件下载到我的电脑)页面"add.php?a=1&b=4"的输出结果。下面是php代码。
代码
read.php
<?php
$filename = "add.php?a=1&b=4";
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
echo $contents; //I hope it can output “5” (the output result of the page “add.php?a=1&b=4”)
fclose($handle);
?>
add.php
<?php
echo $_GET["a"]+$_GET["b"];
?>
下面是readfile
函数的示例。我想下载页面"add.php?a=1&b=4"的输出结果作为文件"result.txt"。但它也不起作用。
readfile.php
<?php
header("Content-type:application");
header("Content-Disposition: attachment; filename=result.txt");
readfile("add.php?a=1&b=4");
?>
如何修改?
如果您想读取web输出,您需要通过HTTP服务器读取它。
<?php
header("Content-type:application");
header("Content-Disposition: attachment; filename=result.txt");
echo file_get_contents("http://yourserver.example.com/add.php?a=1&b=4");
$url = "[absolute url]/add.php?a=1&b=4";
header("Content-Type: application/octet-stream");
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename="result.txt"");
readfile($url);