2012-05-13 31 views
2

我想用PHP调用一个本地PHP页面,并且已经执行了服务器端代码。获取本地PHP页面作为外部客户端

举例来说,如果我有一个页面,内容如下:

Hello, this is <?php $name="John"; echo $name ?> 

我希望有一个get命令如的file_get_contents(local_php_page)或的fopen(句柄)的回报:

Hello, this is John 

而不是

Hello, this is <?php $name="John"; echo $name ?> 

这样做的最佳方法是什么?

回答

1

输出缓冲应该能为你做的:

ob_start(); 
include 'myfile.php'; 
$xhtml = ob_get_clean(); 

你也可以得到输出你的包括,例如:

$xhtml = include 'myfile.php'; 

更多关于这一点,看看The PHP Manual

0

正如您已经提到的,您可以使用file_get_contents向任何http url发送获取请求。您必须确保,allow_url_fopen已启用。当然,你的网络服务器已经进行配置,以处理PHP请求正确

<?php 
$requestUrl = 'http://localhost/your_file.php'; 
$content = file_get_contents($requestUrl); 
var_dump($content); 
0

如果你在你的服务器有allow_url_fopen选项,您可以使用的fopen与网址。

0

需要调用HTTP URL,而不是路径

$url="http://".$_SERVER['HTTP_HOST']."/yourpath/"; 
$str=file_get_contents($url."/yourfile.php"); 
echo $str;