我想加载一个域名url在div元素
我有两个问题:
- 如果加载一个域名,例如'google.com'与http协议不加载地址图像正确,但加载页面!
- 如果用https协议加载谷歌地图,给出警告,永远不工作!
load.php
<html>
<head>
<title></title>
<script type="text/javascript" src='jquery.js'></script>
</head>
<body>
<div></div>
<script type="text/javascript">
$(function(){
$('div').load('/x-request.php?url=googleMapAdress');
});
</script>
</body>
</html>
x-request.php
<?php
echo file_get_contents('https://' . $_GET['url']);
?>
注意:我不想使用iframe标签&我的代码必须有ajax代码!
解决方案是什么?
//更多解释
如何获得任何外部页面和加载它在div元素完全?这意味着=> href, SRC和所有链接都可以正常工作。
我已经试过下面的代码,它的工作如预期的
<html>
<head>
<title></title>
<script type="text/javascript" src='js/jquery.js'></script>
</head>
<body>
<div></div>
<script type="text/javascript">
$(function(){
$('div').load('x-request.php?url=maps.google.com');
});
</script>
</body>
</html>
php代码<?php
echo file_get_contents('https://' . $_GET['url']);
?>
正在加载谷歌地图,没有任何错误。
编辑
对于以下评论的回应是可能的最佳解决方案。
您要查找的设置是allow_url_fopen
。
在php.ini文件中设置allow_url_fopen = On
在不更改php.ini的情况下,有两种方法可以绕过它,一种是使用fsockopen,另一种是使用cURL。
<<p> 旋度例子/strong>function get_content($URL)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $URL);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
echo get_content('http://example.com');
curl的快速参考链接如下。
http://www.kanersan.com/blog.php?blogId=45 http://www.tomjepson.co.uk/enabling-curl-in-php-php-ini-wamp-xamp-ubuntu/<php
$ch = curl_init();// set url
curl_setopt($ch, CURLOPT_URL, $_GET['url']);
//return the as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch,CURLOPT_BINARYTRANSFER, true);
// echo output string
$output = curl_exec($ch);
$url = $_GET['url'];
$path = 'http://'.$url.'/';
$search = '#url'((?!'s*[''"]?(?:https?:)?//)'s*([''"])?#';
$replace = "url($1{$path}";
$output = preg_replace($search, $replace, $output);
echo $output;
// close curl resource to free up system resources
curl_close($ch);