使用 PHP 文件加载外部网站
我正在使用 JQuery 调整 iframe 的大小,使其具有与内容相同的高度,它工作得很好,尽管它仅在内容位于同一域中时才有效。
我需要显示外部内容,因此我使 iframe 指向服务器中的一个文件,该文件又应显示外部内容:
<iframe src="frame.php" scrolling="no" frameborder="0" noresize="noresize" style="width:100%; border: 1px solid black;">
如何使frame.php 显示外部域的内容?
如果您需要更多信息,这里是有关如何操作的答案(他解释了除了如何在 php 文件中显示内容之外的所有内容): 自动高度 IFrame
谢谢
I'm using JQuery to resize an iframe to make it have the same height as it's content, it works perfectly though it only works if the content is in the same domain.
I need to display external content so I'm making the iframe point to a file in my server which in turn should display the external content:
<iframe src="frame.php" scrolling="no" frameborder="0" noresize="noresize" style="width:100%; border: 1px solid black;">
How do I make frame.php display the content of the external domain?
In case you need more info here is the answer on how to do it (he explains everything excepts how to display content in the php file):
AutoHeight IFrame
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不幸的是,php 不是执行此操作的正确语言。为了确定 iframe 中文档的高度,您需要从父文档(
iframe
元素所在的位置)对src< 中引用的文档进行 javascript 调用iframe 的 /code>。
如果 iframe 的 src 位于您的域中,则根据同源策略,您拥有使用文档 DOM 获取文档高度的完全权限。当您从其他域加载文档时,您无权查询高度。您的问题的回答者似乎建议在您的网站上设置代理,以便绕过同源政策。
但是,还会存在其他问题(即代理页面上的相对链接等)
Unfortunately, php isn't the correct language to be doing this. In order to determine the height of the document in the iframe, you would need to make a javascript call from the parent document (where the
iframe
element is) to the document referenced in thesrc
of the iframe.In the case where the
src
of the iframe is on your domain, you have full permission to use the DOM of the document to get the document's height, as per the same origin policy. When you load a document from another domain, you do not have permission to query for the height. What the answerer of your question seems to be suggesting is setting up a proxy on your site so that the same origin policy would be circumvented.However, there going to be other issues with that (ie relative links on the proxied page, etc.)
那么您想获取外部站点的内容吗?
这可以工作,但当目标页面具有非绝对 URL 和相对链接时,它会破坏布局:
frame.php:
So you want to fetch the contents of an external site?
This will work, but it'll break the layout when the target page has non-absolute URLs and relative links:
frame.php:
类似的东西就可以了
<?php echo file_get_contents( "http://example.com" );?>
Somethhing like that will do