php PHP如何调用另一个页面并将输出作为变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7476216/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to PHP call another page and get output as variable?
提问by Yoshiyahu
I would like to call a PHP page from a PHP page, and return the output of the page I called as a single variable.
我想从 PHP 页面调用 PHP 页面,并将我调用的页面的输出作为单个变量返回。
require
would not suit my purposes as I need the output stored as a variable for later use, rather than outputting it immediately.
require
不适合我的目的,因为我需要将输出存储为变量供以后使用,而不是立即输出。
IE:
IE:
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>";
?>
Would output:
会输出:
Hello World!
Go for it!
Is this real life?
采纳答案by mfonda
require
is actually exactly what you want (in combination with output buffering):
require
实际上正是您想要的(结合输出缓冲):
ob_start();
require 'page2.php';
$output = ob_get_clean();
回答by KoE
To clear up some confusion when using file_get_contents()
, note that:
为了消除使用时的一些困惑file_get_contents()
,请注意:
Using with the full urlwill yeild the html outputof the page:
$contents = file_get_contents('http://www.example-domain.com/example.php');
While using it with file pathwill get you the source codeof the page:
$contents = file_get_contents('./example.php');
使用完整的 url将产生页面的html 输出:
$contents = file_get_contents('http://www.example-domain.com/example.php');
将它与文件路径一起使用将为您提供页面的源代码:
$contents = file_get_contents('./example.php');
回答by Dan Ellis
You could use the file_get_contents method, this returns a string containing the contents of the page you request - http://php.net/manual/en/function.file-get-contents.php
您可以使用 file_get_contents 方法,这将返回一个包含您请求的页面内容的字符串 - http://php.net/manual/en/function.file-get-contents.php
$contents = file_get_contents('http://www.stackoverflow.com/');
echo $contents;
回答by Marc B
ob_start();
include('otherpage.php');
$output = ob_get_clean();
A better method would be to encapsulate whatever's being done in that other page as a function, which you can then call from both pages. This lets you reuse the code without having to mess around with output buffering and whatnot.
更好的方法是将其他页面中正在执行的任何操作封装为一个函数,然后您可以从两个页面调用该函数。这使您可以重用代码,而不必处理输出缓冲等问题。
回答by Jose Gómez
In my case, none of the solutions posted worked for me. Only the comment by Ond?ej Mirtesworked. I wanted to execute a script, passing some parameters:
就我而言,发布的所有解决方案都不适合我。只有Ond?ej Mirtes的评论有效。我想执行一个脚本,传递一些参数:
anotherscript.php?param=value
So, the solution was to use cURL:
因此,解决方案是使用 cURL:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http".( ($_SERVER["HTTPS"] == "on")?'s':'' )."://".$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF'])."/anotherscript.php?param=value");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
curl_close($ch);
回答by Andrei
I want to create pdf from existing .php files.
From answers above i have 3 options:
1.rewrite .php files
2.use buffering:
我想从现有的 .php 文件创建 pdf。
从上面的答案我有 3 个选项:
1.rewrite .php 文件
2.use 缓冲:
$_GET["id"]="4";
ob_start();
include 'test.php';
$TextPDF = ob_get_clean();
3.get data by url:
3.通过url获取数据:
$serv = "http".( ($_SERVER["HTTPS"] == "on")?'s':'' )."://".$_SERVER["SERVER_NAME"];
$TextPDF = file_get_contents($serv.'/test.php?id=4');
I think i will use third option, then i don't have to modify existing files.
我想我会使用第三个选项,然后我不必修改现有文件。