调用discuz x论坛新帖的代码,用于自动更新首页
<?php/*
调用论坛的10个最新帖子,输出为html格式,通过计划任务更新,更新时间可以在计划任务中设置
*/
if(!defined('IN_DISCUZ')) {
exit('Access Denied');
}
$newhtml = '';
$query = DB::query("SELECT t.*, f.name FROM pre_forum_thread t, pre_forum_forum f WHERE t.fid=f.fid and f.fid IN('2','32') ORDER BY t.dateline DESC LIMIT 0,10");
while($new = DB::fetch($query)) {
$newsubject = cutstr($new['subject'],42);
$newurl = 'http://yourbbs/thread-'.$new['tid'].'-1-1.html';
$newhtml .= '<li><a target="_blank" href="'.$newurl.'">'.$newsubject.'</a></li>';
}
$newhtml = convert_data($newhtml);
writehtml('newhtml.htm', $newhtml);
//写入html文件
function writehtml($file, $html) {
global $timestamp, $_DCACHE;
$yearmonth = gmdate('Ym', $timestamp + $_DCACHE['settings']['timeoffset'] * 3600);
$logdir = DISCUZ_ROOT.'/data/cache/';
$logfile = $logdir.$file;
if($fp = @fopen($logfile, 'w')) {
@flock($fp, 2);
fwrite($fp, "$html");
fclose($fp);
}
}
function convert_data($data) {
include DISCUZ_ROOT.'/source/class/class_chinese.php';
$c = new Chinese('GBK', 'UTF-8');
$data = $c->Convert($data);
return $data;
}
?>
复制代码
把以上代码,放入记事本文件,然后,把记事本的名称修改为 makehtml_daily.inc.php即php文件。然后把 makehtml_daily.inc.php 文件,通过ftp上传到 /source/include/cron/目录中。
在论坛的后台,工具->计划任务—>增添一个任务,然后点击这个任务的“编辑”,在“任务脚本:”的输入框里输入:makehtml_daily.inc.php
其他更新时间设置,可以自己选择。
然后,通过这个地址:http://yourbbs/data/cache/newhtml.htm 可以获得HTML格式的论坛帖子。注意,请在UTF8编码下查看。
得到的html文件可以用如下代码嵌入首页,仅适用于PHP程序,其它语言程序欢迎大家跟帖!<?php echo file_get_contents("http://yourbbs/data/cache/newhtml.htm"); ?>
复制代码
页:
[1]