Reputation: 89
I want load a domain url in div element
I have 2 problems :
Here is my code :
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']);
?>
Note: I don't want to use iframe tag & my code must have ajax code!
What is the solution?
//more explain
How get any external page and load it in div element completely ? this is means => href, src and all links work truly.
Upvotes: 1
Views: 325
Reputation: 27364
I have tried you below code and its working as expected.
<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 code
<?php
echo file_get_contents('https://' . $_GET['url']);
?>
Its loading Google map without any error.
Edit
In response of comment following are the possible best solutions.
The setting you are looking for is allow_url_fopen
.
set allow_url_fopen = On
in php.ini file.
You have two ways of getting around it without changing php.ini, one of them is to use fsockopen, and the other is to use cURL.
curl example
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');
Quick reference links are as below for 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);
Upvotes: 1