我找到了一个在 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
问题:
如何让它在我的 iframe 中显示图像和数据? (当前出现 404 错误,因为它试图在我的网络服务器上查找图像)
如何在此 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/