我想要做的是将网站的HTML存储在php变量中,以便在某种情况下回显它。我的PHP代码:
<?php
$url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$URL = "http://www.example.com/";
$URL2 = "http://www.example2.com/";
$website1 = ?
$website2 = ?
if($url == $URL) {
echo $website1;
} else if($url == $URL2) {
echo $website2;
}
?>
我希望将此HTML(下方)存储在$website1
中
还会在$website2
<html>
<head>
<link rel='stylesheet' type='text/css' media='all' href='css/style.css'>
<link rel='stylesheet' type='text/css' media='all' href='css/jquery-ui-1.10.4.custom.css'>
<title>Website #One</title>
<script type='text/javascript' src='js/jquery-1.9.1.js'></script>
<script type='text/javascript' src='js/jquery-ui-1.10.4.custom.js'></script>
<script type='text/javascript' src='js/script.js'></script>
</head>
<body>
<div id='header'></div>
<div id='menu'>
<button id='navbtn' class='active btn'>Home</button><button id='navbtn2' class='btn'>Pictures</button><button id='navbtn3' class='btn'>Videos</button><button id='navbtn4' class='btn'>Games</button><button id='navbtn4' class='btn'>About</button><button id='navbtn5' class='btn'>Contact</button>
</div>
<div id='content'></div>
</body>
</html>
我怎样才能实现这个目标?
答案 0 :(得分:2)
我不了解您正在使用的逻辑,但您可以使用PHP的内置函数file_get_contents()
来实现这一点。使用这种方式:
<?php
$url = "http://".getenv("HTTP_HOST")."/".getenv("REQUEST_URI");
$URL = "http://www.example.com/";
$URL2 = "http://www.example2.com/";
$website1 = file_get_contents($URL);
$website2 = file_get_contents($URL2);
if($url == $URL) {
echo $website1;
} else if($url == $URL2) {
echo $website2;
}
?>
答案 1 :(得分:0)
您可以使用file_get_contents();
或者CURL库
http://php.net/manual/fr/book.curl.php
答案 2 :(得分:0)
Curl是一种很好的方法:http://www.digimantra.com/technology/php/get-data-from-a-url-using-curl-php/
<?php
$url='http://twitter.com/statuses/user_timeline/16387631.json'; //rss link for the twitter timeline
print_r(get_data($url)); //dumps the content, you can manipulate as you wish to
/* gets the data from a URL */
function get_data($url)
{
$ch = curl_init();
$timeout = 5;
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
?>
答案 3 :(得分:0)
谢谢你们,我能够从你们的答案中学习并设法让它在这里工作是我最后的PHP代码:
<?php
$get_url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$url1 = "http://thedag.ga/thedagga/main.php";
$url2 = "http://thedag.ga/zp/zp18.php";
$tdurl1 = "http://thedag.ga/";
$tdurl2 = "http://www.thedag.ga/";
$zpurl1 = "http://zp.thedag.ga/";
$zpurl2 = "http://zp18.thedag.ga/";
$zpurl3 = "http://zp4rker.thedag.ga/";
if($get_url == $tdurl1 || $get_url == $tdurl2) {
print_r(file_get_contents($url1));
} else if($get_url == $zpurl1 || $get_url == $zpurl2 || $get_url == $zpurl3) {
print_r(file_get_contents($url2));
}
?>
答案 4 :(得分:-1)
为什么不把两个版本放在自己的PHP文件中,也就是说,如果它只是一些简单的单页html,而不是整个网站,那么其他建议可能会更好。
<?php
$url = "http://".getenv("HTTP_HOST")."/".getenv("REQUEST_URI");
$URL = "http://www.example.com/";
$URL2 = "http://www.example2.com/";
if($url == $URL) {
include 'header1.php';
} else if($url == $URL2) {
include 'header2.php';
}
?>