如何在 PHP 中包含外部文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4191364/
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 do I include an external file in PHP?
提问by Sean
I need to include and external file that is on another url. For example google.com. I have tested the include using local files, so that much works, but if I try and use 127.0.0.1/filetoinclude.txt Nothing happens. I don't get an error, I just get a blank page. So how am I supposed to include http://google.comin my page?
我需要包含另一个 url 上的外部文件。例如 google.com。我已经使用本地文件测试了包含,所以很多工作,但如果我尝试使用 127.0.0.1/filetoinclude.txt 什么也没有发生。我没有收到错误,我只是得到一个空白页。那么我应该如何在我的页面中包含http://google.com呢?
回答by Horatio Alderaan
I have no idea why you'd want to do this, but you could most certainly try something like:
我不知道您为什么要这样做,但您肯定可以尝试以下操作:
<?php
$google_page = file_get_contents('http://www.google.com');
echo $google_page;
?>
回答by Jacob Relkin
You'll need to use file_get_contents
:
你需要使用file_get_contents
:
$data = file_get_contents('http://google.com'); //will block
Or fopen
:
或者fopen
:
$fp = fopen('http://google.com', 'r');
$data = '';
while(!feof($fp))
$data .= fread($fp, 4092);
fclose($fp);
echo $data;