我一直试图解决这个问题很长一段时间了。我有一个朋友的独特请求,帮助他们构建一个动态从外部页面(不同的服务器)提取div标签的网页,以便在该页面更新时,它会在此页面上更新。我通过利用jquery的.load指定要引入的.div来解决这个问题,但样式也不会流入,因为它不在div标签本身内,而是在标签内。所以,我试图弄清楚如何从其他页面中提取内容,特别是样式表(如果可能的话),并将其放在我页面的标签中。
我对此感到茫然,我真的很感激一些帮助。
这是我的页面设置,因为我现在拥有它:
proxy.php(拉入外部页面)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
<base target="_blank">
</head>
<body>
<?php
$url = 'www.EXAMPLE.com/';
$htm = file_get_contents($url);
echo $htm;
?>
</body>
</html>
显示特定内容的实际页面:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title> - Products</title>
<?php
$file = file_get_contents("proxy.php");
$head = preg_replace("#(.*)<head>(.*?)</head>(.*)#is", '$2', $file);
echo $head;
?>
<script language="javascript" type="text/javascript" src="js/jquery-1.9.0.min.js"></script>
<link href="css/main.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="wrapper">
<div id="contentWrapper">
<div id="contentMain">
<script>
$("#contentMain").load("proxy.php #content");
</script>
</div>
</div>
</div>
</body>
<!-- InstanceEnd -->
</html>
答案 0 :(得分:0)
您可以为远程文件创建file_get_contents
的cURL版本,因为出于安全原因,很可能未在主机服务器上设置allow_url_fopen
。
function curl_file_get_contents($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, 12);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$curlError = curl_errno($ch);
curl_close($ch);
// Return result if we don't have errors, if there is a result, and if we have a 200 OK
if (!$curlError && $result && $status == '200') {
return $result;
} else {
return false;
}
}
$stylesheet = curl_file_get_contents('http://www.example.com/css/style.css');
echo "<pre>";
var_dump($stylesheet);
echo "</pre>";