Search code examples
phpwordpressfile-get-contents

file_get_contents not returning entire site


I have been trying to retrieve the contents of a website (https://www.programsgulf.com/) using file_get_contents. Unfortunately, the resulting output is missing many elements (images, formating, styling, etc...), and just basically looks nothing like the original page I'm trying to retrieve.

This has never happened before with any other URLs I have tried retrieve using this same method, but for some reason, this particular URL (https://www.programsgulf.com/) refuses to work properly.

The code I'm using is:

<?php
$homepage = file_get_contents('https://www.programsgulf.com/');
echo $homepage;
?>

Am I missing anything? All suggestions on how to get this working properly would be greatly appreciated. Thank you all for your time and consideration.


Solution

  • You can't just echo someone's html and expect it to work. Assets (like scripts, images or stylesheets) won't load due to same-origin policy violation unless the server has (mis)configured CORS rules. This is a protection layer in every modern browser that you won't overcome.

    If you really want this to work you have to download each asset on the server side, store them locally and replace links in the code to your local copies. This is exactly how web scraping/online proxy software work.