如何使用 php 代理将网站加载到 iframe 中?

Bla*_*ack 4 html javascript php iframe web

我尝试将我在 URL 中设置的随机页面加载到 iframe 中。假设我尝试在 iframe 中加载“ https://www.google.de ”。

<iframe src="<?php echo $_GET['URL'];?>" width="600" height="500"></iframe>
Run Code Online (Sandbox Code Playgroud)

这是我设置 URL 的方法:

localhost/test/index.php?URL=https://www.google.de
Run Code Online (Sandbox Code Playgroud)

仅加载空白页。我知道其原因是 Google 正在发送“X-Frame-Options: SAMEORIGIN”响应标头。

但是,我尝试找到一种方法来加载它,有人告诉我使用 PHP 代理脚本来执行此操作,但我进行了研究,发现没有任何帮助。

我该如何解决这个问题?

joy*_*nks 5

如果您位于任何代理服务器后面并且想要使用代理 url、域、用户名、密码进行身份验证来绕过它,请使用此脚本取消注释代理行。

我使用了header("Access-Control-Allow-Origin: *");因为我从不同的域调用这个脚本。

文件:proxyscript.php

<?php
    header("Access-Control-Allow-Origin: *");

    echo get_page($_GET['url']);
    //echo get_page("http://www.google.com");
    function get_page($url){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,$url);
        /*
        $proxy = 'http://proxy.company.com:8080';
        $proxyauth = 'domain\proxy_username:proxy_password';
        curl_setopt($ch, CURLOPT_PROXY, $proxy);
        curl_setopt($ch, CURLOPT_PROXYUSERPWD, $proxyauth);
        */
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        $data = curl_exec($ch);
        curl_close($ch);
        return $data;
    }
?>
Run Code Online (Sandbox Code Playgroud)

文件:index.html

<html>
    <head>
        <script>
            function getURL(url){
                document.getElementById('frame').src 
                   = 'path/to/proxyscript.php?url='+encodeURIComponent(url);
            }
        </script>
    </head>
    <body>
        <button onclick="getURL( 'http://www.google.com')">Google</button>
        <iframe id="frame" src=""></iframe>
    </body>
</html> 
Run Code Online (Sandbox Code Playgroud)