[英]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.