javascript - 如何通过我的网络服务器创建一个使用 php 作为代理的外部站点 iframe? (避免 CORS)

标签 javascript php html curl cross-domain

我找到了一个在 php 中使用 curl 的示例,这是我目前所做的:

index.html:

<!DOCTYPE html>
<html>
<head>
</head>
<body>
<iframe src="fetch.php" width="800" height="500"></iframe>
</body>
</html>

获取.php:

    ini_set ( "allow_url_fopen", "On" );

    $url = "http://www.google.com/";
    $ch = curl_init ();
    curl_setopt($ch,CURLOPT_FOLLOWLOCATION,true);
    curl_setopt ( $ch, CURLOPT_URL, $url );
    curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
    $data = curl_exec ( $ch );
    curl_close ( $ch );
    echo $data;

这将显示:http://i.imgur.com/0dUfrnA.jpg

问题:

  1. 如何让它在我的 iframe 中显示图像和数据? (当前出现 404 错误,因为它试图在我的网络服务器上查找图像)

  2. 如何在此 iframe 中导航?例如,如果我在那个 iframe 中搜索谷歌,我得到:http://i.imgur.com/ly97aUX.jpg

最佳答案

您的代码所做的是在给定的 url(在本例中为“http://www.google.com/”)下载页面源代码。这实际上与转到网页、单击“查看源代码”并复制/粘贴源代码相同。

如果您希望保留页面的原始外观以及在 iframe 中导航的能力,那么您不需要 cURL,您只需将 iframe 更改为指向该页面。

请注意,它不适用于 www.google.com,因为它会在响应中发送“X-Frame-Options: SAMEORIGIN” header ,阻止 iframe 包含,至少在兼容的浏览器中是这样。

关于javascript - 如何通过我的网络服务器创建一个使用 php 作为代理的外部站点 iframe? (避免 CORS),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21895377/

相关文章:

javascript - Tomcat 5.5 的跨源资源共享

php - 在php中从网站的源代码中获取数据

css - CSS 绝对定位之谜

javascript - 在 WordPress 页面中嵌入 Tradingview 小部件

php - 如何在php中打印多维数组

PHP 数组到 javascript 的转换改变了索引值

html - :target pseudo selector not selecting target

javascript - Webapi Controller 参数为空

Javascript 动画打开/关闭菜单

javascript - Angular - 图像不会显示