[英]I want to create several static html pages from my PHP content using a wwwcopy script
于2012年7月16日编辑
我需要重新考虑这个问题。 我使用动态页面已有一段时间,并在上面使用了缓存的PHP。 我的大部分页面都已缓存,但没有元信息。 正如我在下面的帖子中所解释的,我需要减少/删除对本地MySQL数据库的请求。 由于即将到来的请求,我收到500个内部服务器错误。 我一直在探索Jquery / Ajax,将数据从数据库中提取到xml页面中,并从该站点请求数据,这是可行的,但是我仍然遇到将内容从主体移到meta的问题。 Facebook,搜索引擎可以看到动态内容。 这是我需要的........
我需要一种解决方案,该解决方案可以通过查看http://chennaichristianradio.com/PHP/web/songinfo.php?songID=来创建大约1500个静态页面,并以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
谢谢大家的帮助! -鲍勃·斯威格蒂
原始帖子下方
我想使用以下脚本创建数百个静态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
?>
使用此功能可以完成PHP缓存,但是我的问题是它仅缓存此代码及以下代码中的PHP信息。 这是一个问题,是因为我在“开放图形”标签的元信息aslo中使用了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返回服务器的流量。 希望这可以澄清我的需求,并感谢大家的回应和帮助。
我不确定您要实现的目标,但是要执行1200次,只需使用for循环并更改文件名和网址即可。
for ($i = 1; $i <= 1200; $i++)
{
wwwcopy("url", "file");
}
我认为您必须选择缓存选项。 如果使用任何框架,则有用于启用缓存的默认设置。 这确实可以提高您的速度。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.