How to include an external file in PHP?

I need to include an external file, which is located on a different URL. For example, google.com. I tested include using local files, so it works a lot, but if I try to use 127.0.0.1/filetoinclude.txt, nothing happens. I have no error, I just get a blank page. So how should I include http://google.com on my page?

+3
source share
2 answers

I have no idea why you need this, but you can probably try something like:

<?php
    $google_page = file_get_contents('http://www.google.com');
    echo $google_page;
?>
+11
source

You will 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;
+3

Source: https://habr.com/ru/post/1775112/


All Articles