简体   繁体   English

加载 <head></head> 来自外部页面的内容,特别是来自外部页面的样式表

[英]Load <head> </head> content from external page, specifically the stylesheets from the external page

I have been trying to solution this for quite some time now. 我已经尝试解决这个问题了一段时间了。 I have had a unique request from a friend to help them build a webpage that dynamically pulls a div tag from an external page (different server) so that when that page updates, it updates on this page. 我有一个朋友的独特要求,以帮助他们建立一个网页,该网页从外部页面(不同的服务器)动态提取div标签,以便该页面更新时在此页面上更新。 I solved that by utilizing jquery's .load to specify the .div to pull in, but the styling does not flow in as well since it does not reside within the div tag itself, but within the tags. 我通过利用jquery的.load指定要插入的.div来解决此问题,但是样式不是很好地传入,因为它不位于div标签本身内,而是位于标签内。 So, I am trying to figure out how I can pull in the content, specifically the stylesheets if at all possible, from the other page and place it in the tags of my page. 因此,我试图弄清楚如何从其他页面提取内容,尤其是样式表,并将其放在页面的标签中。

I am at a loss on this and I would really appreciate some help. 我对此感到茫然,我将不胜感激。

Here is my page setup as I have it this moment: 这是我现在的页面设置:

proxy.php (pulls external page in) 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>

Actual page to display specific content: 显示特定内容的实际页面:

<!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>

You can create a cURL version of file_get_contents for remote files because allow_url_fopen most likely isn't set on your host's server for security reasons. 您可以为远程文件创建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>";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM