I have a Chrome extension that comes with a popup window which, among other things, contains an iframe
to load websites. In principle, I can set the src
attribute of the iFrame and it works fine. The problem, however, is that many website do not allow to be loaded within frame, yielding a Refused to display '...' in a frame because it set 'X-Frame-Options' to 'SAMEORIGIN'
error. Fair enough!
After some looking around I found a PHP/cURL solution to uses a simple scrip to fetch the content of a URL using cURL and returning the content, which in turn is then used as value for the src
attribute. Here's the script grabber.php
:
<?php
$ch = curl_init($_GET['url']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
$content = curl_exec($ch);
curl_close($ch);
echo $content;
?>
And here's the code I use to set the src
attribute of the iFrame:
$('#myIFrame').attr('src', './grabber.php?url='+someURL);
Again, this work in principle, but only reasonably well for rather static / plain HTML websites. As soon as the website is very dynamic, I only get some bits and pieces, i.e., missing content and/or missing style sheets. In short, what the iFrame shows has often almost nothing to do with how the website really looks like.
Is there any chance to get this working properly for any website?