于2012年7月16日编辑
我需要重新审视这个问题。我已经使用动态页面一段时间使用缓存的PHP了一段时间。我的大多数页面都是缓存的,但元信息却没有。正如我在下面的帖子中解释的那样,我需要减少/删除发往我本地MySQL数据库的请求。由于即将发出请求,我收到500个内部服务器错误。我一直在探索Jquery / Ajax将数据从数据库中提取到xml页面并从中获取站点请求数据,这是有效的,但我仍然遇到问题,将内容从正文移动到元数据中Facebook和搜索引擎可以看到动态内容。这就是我需要的........
我需要一个解决方案,通过查看创建大约1500个静态页面 http://chennaichristianradio.com/PHP/web/songinfo.php?songID=从0开始计数到1500(http://chennaichristianradio.com/PHP/web/songinfo.php?songID=0到http://chennaichristianradio.com/PHP/web/songinfo.php?songID=1500)。这对我有用,但不是首选。
第二个选项是使用PHP缓存选项来缓存整个页面,包括从PHP创建的元数据。
第三个选项(我的首选选项)是能够从正文中的ajax / Jquery创建的数据或xml页面本身创建元数据。
感谢您重新访问此请求。请阅读我今年早些时候的原帖。这是我正在使用的一些当前代码和示例的链接.....
当前用于创建动态页面的PHP ...
<title>Chennai Christian Radio</title>
<meta property="og:title" content="<?php echo $song->title; ?> by <?php echo $song->artist; ?> - Found on Chennai Christian Radio"/>
<meta property="og:type" content="song"/>
<meta property="og:url" content="http://chennaichristianradio.com/PHP/web/songinfo.php?songID=<?php echo $song->ID; ?>"/>
<meta property="og:image" content="<?php echo $song->picture; ?>"/>
<meta property="og:site_name" content="Chennai Christian Radio"/>
<meta property="fb:admins" content="1013572426"/>
<?php
$cachefile = "cache/".$reqfilename.$cache_folder.md5($_SERVER['REQUEST_URI']);
$cachetime = 11000 * 60; // 110000 minutes
// Serve from the cache if it is younger than $cachetime
if (file_exists($cachefile) && (time() - $cachetime
< filemtime($cachefile)))
{
include($cachefile);
echo "<!-- Cached ".date('jS F Y H:i', filemtime($cachefile))."
-->n";
exit;
}
ob_start(); // start the output buffer
?>
<div id="fbbutton">
<div id="fb-root"></div>
<script>(function(d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) return;
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/en_US/all.js#xfbml=1&appId=170819122971839";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));</script>
<div class="fb-like" data-send="false" data-layout="box_count" data-width="50" data-show-faces="false"></div>
</div>
<div id="songinfo">
<!-- BEGIN:AlbumArt -->
<?php if(SHOW_PICTURES && !empty($song->picture)) : ?> <img class="picture" id="picture" onload="showPicture(this, <?php echo SHOW_PICTURES; ?>)" src="<?php echo $song->picture; ?>" alt="" border=0 width="142" height="142" /></a><?php endif; ?>
</div>
<!-- Song Info -->
<div id="wrapper">
<div id="title"><?php echo $song->title; ?></div>
<div id="artist"><?php echo $song->artist; ?></div>
<div id="album"><?php echo $song->album; ?></div>
<div id="duration"><?php echo $song->durationDisplay; ?></div>
<div id="year"><?php echo $song->albumyear; ?></div>
</div>
<div id="lyrics"><pre><?php if (!empty($song->lyrics)) : ?><dd class="broad"><pre><?php echo $song->lyrics; ?><?php endif; ?></pre></div>
<?php
// open the cache file for writing
$fp = fopen($cachefile, 'w');
// save the contents of output buffer to the file
fwrite($fp, ob_get_contents());
// close the file
fclose($fp);
// Send the output to the browser
ob_end_flush();
?>
这是我创建XML的新脚本.......
<?php
// Change to your database user name
$username="*********";
//Change to your database password
$password="*********";
// Change to your database name
$database="********";
// Connect to the database
mysql_connect('*********',$username,$password);
// Handle an error
@mysql_select_db($database) or die( "Unable to select database");
// Build Sql that returns the data needed - change this as required.
$sql = "SELECT songlist.*, historylist.listeners as listeners, historylist.requestID as requestID, historylist.date_played as starttime FROM historylist,songlist WHERE (historylist.songID = songlist.ID) AND (songlist.songtype='S' OR songlist.songtype='C' OR songlist.songtype='N') ORDER BY historylist.date_played DESC LIMIT 1";
// Store results in result object
$result = mysql_query($sql);
//store values in vars for calculation for array creation
$curPlay = mysql_query("SELECT * FROM historylist ORDER BY date_played DESC LIMIT 1");
$curPlayRow = mysql_fetch_assoc($curPlay);
if (!$curPlay) { echo( mysql_error()); }
$dt_duration = mysql_result($result,$i,'duration');
$title= mysql_result($result,$i,'title');
$artist= mysql_result($result,$i,'artist');
$album= mysql_result($result,$i,'album');
$picture= rawurlencode(mysql_result($result,$i,'picture'));
$lyrics= mysql_result($result,$i,'lyrics');
$ID= mysql_result($result,$i,'ID');
$curtime = time();
$timeleft = $starttime+round($dt_duration/1000)-$curtime;
$secsRemain = (round($dt_duration / 1000)-($curtime-$starttime));
//build array to return via getXMLHTTPRequest object - you can include more vars but remeber to handle them
//correcty in the useHttpResponse function
$Aj_array = $dt_duration . '|' . $secsRemain . '|' . $title . '|' . $artist .'|' . $album .'|' . $picture .'|' . $lyrics .'|' . $ID;
//we are using the text response object - which i think is easier for small data return
//just echo the array - not so much AJAX rather AJA ??
echo $Aj_array
?>
我的html中的脚本可以读取提取的内容....
<script language="JavaScript">
var http = getXMLHTTPRequest();
var counter;
function getXMLHTTPRequest() {
try {
req = new XMLHttpRequest();
} catch(err1) {
try {
req = new ActiveXObject("Msxml2.XMLHTTP");
} catch (err2) {
try {
req = new ActiveXObject("Microsoft.XMLHTTP");
} catch (err3) {
req = false;
}
}
}
return req;
}
function getServerText() {
var myurl = 'http://bangalorechristianradio.com/PHP/web/aj.php';
myRand = parseInt(Math.random()*999999999999999);
var modurl = myurl+"?rand="+myRand;
http.open("GET", modurl, true);
http.onreadystatechange = useHttpResponse;
http.send(null);
}
function useHttpResponse() {
if (http.readyState == 4) {
if(http.status == 200) {
var aj_results = http.responseText.split("|");
var aj_duration = aj_results[0];
var aj_secsremaining = aj_results[1];
var aj_title = aj_results[2];
var aj_artist = aj_results[3];
var aj_album = aj_results[4];
var aj_picture = aj_results[5];
var aj_lyrics = aj_results[6];
var aj_ID = aj_results[7];
// update title and artist divs
document.getElementById('title').innerHTML = aj_title;
document.getElementById('artist').innerHTML = aj_artist;
document.getElementById('album').innerHTML = aj_album;
document.getElementById('picture').innerHTML = "<img src=http://chennaichristianradio.com/images/album_art/" + aj_results[5] +" width='142' height='142'>"
document.getElementById('lyrics').innerHTML = aj_lyrics;
countDownInterval = aj_secsremaining - 0;
countDownTime = countDownInterval + 1;
countDown()
}
} else {
//document. getElementById('actual').innerHTML = "";
}
}
function countDown() {
countDownTime--;
if (countDownTime == 0) {
countDownTime = countDownInterval;
getServerText()
}
else if (countDownTime < 0)
countDownTime = 30;
if (document.all)
document.all.countDownText.innerText = secsToMins(countDownTime);
else if (document.getElementById)
document.getElementById("countDownText").innerHTML = secsToMins(countDownTime);
stopCountDown();
counter = setTimeout("countDown()", 1000);
}
function secsToMins(theValue) {
var theMin = Math.floor(theValue / 60);
var theSec = (theValue % 60);
if (theSec < 10)
theSec = "0" + theSec;
return(theMin + ":" + theSec);
}
function stopCountDown()
{
clearTimeout(counter)
}
</script>
一些链接 使用PHP动态创建页面:http://chennaichristianradio.com/PHP/web/songinfo.php?songID=5351
XML页面,显示我在网页中需要的数据:http://bangalorechristianradio.com/PHP/web/aj.php
使用JQuery和Ajax创建的页面:http://bangalorechristianradio.com/PHP/web/player.html
感谢大家的帮助!! -Bob Swaggerty
原帖
我想使用以下脚本创建数百个静态html页面:
<?php
function wwwcopy($link,$file)
{
$fp = @fopen($link,"r");
while(!feof($fp))
{
$cont.= fread($fp,1024);
}
fclose($fp);
$fp2 = @fopen($file,"w");
fwrite($fp2,$cont);
fclose($fp2);
}
wwwcopy("http://www.domain.com/list.php?member=sample", "sample.html");
我创建了一个名为create.php的php页面,它创建了1个文件。如何使用此脚本并创建1200页?
我这样做的目的是最大限度地减少发送到我的MySQL数据库的请求,并使我的页面更快地出现。创建这些页面后,我将使用另一个脚本指向这些文件。
(2012年3月24日更新)
感谢您的回复。这是我在这样做时想要完成的。也许有更好的解决方案......
查看我的页面http://chennaichristianradio.com/PHP/web/songinfo.php?songID=5351
我的软件是从ID 5351(我的数据库中的歌曲)动态创建此页面 我正在使用php来获取歌曲信息。为了使这个过程更有效率,我设置了PHP缓存。我正在使用....来缓存这个....
<?php
$cachefile = "cache/".$reqfilename.$cache_folder.md5($_SERVER['REQUEST_URI']);
$cachetime = 11000 * 60; // 110000 minutes
// Serve from the cache if it is younger than $cachetime
if (file_exists($cachefile) && (time() - $cachetime
< filemtime($cachefile)))
{
include($cachefile);
echo "<!-- Cached ".date('jS F Y H:i', filemtime($cachefile))."
-->n";
exit;
}
ob_start(); // start the output buffer
&GT;
PHP缓存正在使用此完成,但我的问题是它只缓存此代码及以下的PHP信息。这是一个问题的原因,是因为我在我的Open Graphic标签的元信息中使用PHP。 OG让人们可以在Facebook上“喜欢”我的音乐。这是我的OG标签的样子。
<title>Chennai Christian Radio</title>
<meta property="og:title" content="<?php echo $song->title; ?> by <?php echo $song->artist; ?> - Found on Chennai Christian Radio"/>
<meta property="og:type" content="song"/>
<meta property="og:url" content="http://chennaichristianradio.com/PHP/web/songinfo.php?songID=<?php echo $song->ID; ?>"/>
<meta property="og:image" content="<?php echo $song->picture; ?>"/>
<meta property="og:site_name" content="Chennai Christian Radio"/>
<meta property="fb:admins" content="1013572426"/>
<meta property="og:description"
content="Chennai Christian Radio is your last stop for today's best Christian Music. http://chennaichristianradio.com"/>
那么......缓存动态页面的解决方案是什么,包括元信息。这绝对是最好的选择,但是使用我当前的代码,它仍然在我的MYSql服务器中查询元信息和歌曲信息。我想通过为此创建静态页面并告诉我的软件指向这些页面而不是查询我的数据库它会更有效率,以及帮助减少我的PHP流量回到我的服务器。希望这能澄清我的需要,并感谢大家的回应和帮助。
答案 0 :(得分:0)
我不确定你想要实现的目标,但这样做1200次只需使用for循环并更改文件名和网址。
for ($i = 1; $i <= 1200; $i++)
{
wwwcopy("url", "file");
}
答案 1 :(得分:0)
我认为你必须选择缓存选项。如果您正在使用任何框架工作,则启用缓存的默认设置。这将真正提高你的速度。