gpt4 book ai didi

php - 如何 PHP 调用另一个页面并将输出作为变量?

转载 作者:可可西里 更新时间:2023-10-31 22:05:34 26 4
gpt4 key购买 nike

我想从一个 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?

最佳答案

为了消除使用 file_get_contents() 时的一些困惑,请注意:

  1. 使用完整 url 将生成页面的 html 输出:

    $contents = file_get_contents('http://www.example-domain.com/example.php');
  2. 将它与文件路径 一起使用会得到页面的源代码:

    $contents = file_get_contents('./example.php');

关于php - 如何 PHP 调用另一个页面并将输出作为变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7476216/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com