3

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.com in my page?

Sean
  • 8,401
  • 15
  • 40
  • 48

2 Answers2

12

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;
?>
chrki
  • 6,143
  • 6
  • 35
  • 55
Horatio Alderaan
  • 3,264
  • 2
  • 24
  • 30
  • Keep in mind that when you use `file_get_contents` to retrieve external domain data you must have `allow_url_fopen = On` in the php.ini and that the fetch is done by the server. Meaning the client's session state on the remote site, the requesting remote address, etc, will be the server's information instead. – Will B. Apr 04 '14 at 20:21
3

You'll need to use file_get_contents:

$data = file_get_contents('http://google.com'); //will block

Or fopen:

$fp = fopen('http://google.com', 'r');
$data = '';
while(!feof($fp)) 
   $data .= fread($fp, 4092); 
fclose($fp); 

echo $data;
Jacob Relkin
  • 161,348
  • 33
  • 346
  • 320