0

私はかなり長い間これを解決しようとしてきました。友人から、外部ページ (別のサーバー) から div タグを動的に取得する Web ページを作成して、そのページが更新されたときにこのページで更新されるようにするための独自の要求がありました。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>
4

1 に答える 1

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>";
于 2013-02-04T12:37:09.327 に答える