在 PHP 中打印 GET 对同一服务器的响应


Print the response of a GET to the same server in PHP

我有两个文件:test.php和query.php。我的服务器是轻的。

在测试中.php我想回显对查询.php请求的响应。

它不起作用,因为Lighttpd是单线程的。

我该怎么做?

如果您真的尝试模拟对页面的请求,那么您应该使用 CURL 之类的东西,或者 file_get_contents .如果您只是尝试在 query.php 中运行代码,请考虑包含/要求查询.php并进行函数/方法调用。

作为快速而肮脏的最后一个选项,您始终可以使用捕获包含查询的输出的输出缓冲区.php:

$original_get = $_GET;
$_GET = array('var1'=>1);
ob_start();
include 'query.php';
$query_contents = ob_get_contents();
ob_end_clean();
$_GET = $original_get;
echo $query_contents;

print_r($_GET(; 将转储 GET 变量。

.php <?php print_r($_GET) ?>进行测试。然后从 query.php,转到

/test.php?var1=val1&var2=val2

测试.php将回显 var1 = val1 和 var2 = val2

您也可以尝试var_dump($_GET)

您可以使用 file_get_contents(( 或 curl 扩展来发送 HTTP 请求。

它可以像这样简单:

echo file_get_contents('http://localhost/query.php?a=b');