PHP: 在服务器端渲染 iframe

PHP: render iframe on the server side

我了解 iframe 是由浏览器引擎呈现的
有没有办法在服务器端呈现完整的 HTML 并将其提供给前端?
我尝试了 PHP's file_get_contents() 函数和 iframe

srcdoc 属性

这只会下载页面内容并使其无法使用

<?= file_get_contents('http://dns_blocked_by_isp.com'); ?>

这会呈现一个无法正常工作的 iframe

<iframe srcdoc="<?= file_get_contents('http://dns_blocked_by_isp.com'); ?>"
        frameborder=0 width=510 height=400 scrolling=no
        allowfullscreen=allowfullscreen>
</iframe>

所以我基本上是在 ASP.NET 中寻找 runat="server" 的替代品,但如果可能 PHP

动机:

我的 ISP 阻止了我从中获取 iframe 源的 DNS 地址,但我的服务器在不同的区域运行,这意味着它可以很好地下载内容并且 iframe 中的 url 具有 -cdn.com 后缀又不会被ISP屏蔽

谢谢

要解决浏览器支持问题,您最好设置一个 PHP 代理页面(即您服务器上的一个脚本,它只获取远程页面并直接将源作为页面提供来自您的服务器),但我怀疑您 运行 遇到的问题只是从远程页面返回的 " 字符破坏了 srcdoc 属性。假设远程文件如下所示:

<p class="worldclass">Hello World</p>

那么您的示例将导致:

<iframe srcdoc="<p class="worldclass">Hello World</p>"

所以 srcdoc 的值就是 <p class=

您只需适当转义代码即可:

<iframe srcdoc="<?= htmlspecialchars(file_get_contents('http://dns_blocked_by_isp.com')); ?>" frameborder=0 width=510 height=400 scrolling=no allowfullscreen=allowfullscreen> </iframe>