从 php 文件调用另一个 php 文件,同时给它一个参数

bio*_*iox 2 php post

我将用一个简单的例子来解释:

myphp1.php:

$html = get_html("myphp2.php", "parameter1"); //pseudocode
Run Code Online (Sandbox Code Playgroud)

myphp2.php

<html>
  <head>
  </head>
  <body>
    <?php
      echo $_POST["parameter1"];
    ?>
  </body>
</html>
Run Code Online (Sandbox Code Playgroud)

所以基本上$html将保存 myphp2.php html 输出。我可以这样做吗?

Dav*_*ude 5

如果您想要解释 php 脚本并保存输出,您应该发送一个新请求。

使用 PHP5,您可以在不使用curl 的情况下执行此操作:

$url = 'http://www.domain.com/mypage2.php';
$data = array('parameter1' => 'value1', 'parameter2' => 'value2');

$options = array(
    'http' => array(
        'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
        'method'  => 'POST',
        'content' => http_build_query($data),
    ),
);
$context  = stream_context_create($options);
$html = file_get_contents($url, false, $context);

var_dump($html);
Run Code Online (Sandbox Code Playgroud)