【发布时间】:2012-03-23 09:15:22
【问题描述】:
于 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
感谢大家的帮助!! -鲍勃·斯瓦格蒂
下面是原帖
我想使用以下脚本创建数百个静态 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 Cache 正在使用它来完成,但我的问题是它只缓存此代码及以下代码中的 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 流量回到我的服务器。希望这能澄清我的需求,我感谢大家的回应和帮助。
【问题讨论】:
标签: php mysql facebook metadata