本贴介绍phpmps调用discuz x方法(非JS),也就是调用静态的帖子到,phpmps首页,这对seo非常必要。
phpmps是一款非常实用的php分类信息开源系统,前些天我给一个网站采用了该系统,在首页调用discuz x2.5论坛内容,但是一直没有找到方法,phpmps官方的论坛人少之又少,在此写个phpmps调用discuz x方法,该方法同样适用于ecshop等其他开源程序的调用,下面不多说废话了,入正题:
具体方法:
新建一个文本文件,把以下代码保存在文本文件中,然后把它重命名为cron_makehtml_daily.php,即php文件,然后通过ftp工具把cron_makehtml_daily.php上传到论坛目录下的/source/include/cron/目录中.
- <?php
- if(!defined('IN_DISCUZ')) {
- exit('Access Denied');
- }
- $newhtml = '';
- $query = DB::query("SELECT t.*, f.name FROM bbs_forum_thread t, bbs_forum_forum f WHERE t.fid=f.fid and f.fid IN('2','36','37','39','40','42') ORDER BY t.dateline DESC LIMIT 0, 10"); //这里要修改你自己想要调用的版块ID
- while($new = DB::fetch($query)) {
- $newsubject = cutstr($new['subject'],48);
- $newurl = 'http://www.100de.com.cn/bbs/thread-'.$new['tid'].'-1-1.html';
- $newhtml .= '<li><a target="_blank" href="'.$newurl.'">'.$newsubject.'</a></li>';
- }</p><p>$newhtml = convert_data($newhtml);
- writehtml('newhtml.htm', $newhtml);
- //写入html文件
- function writehtml($file, $html) {
- global $timestamp;
- $yearmonth = gmdate('Ym', $timestamp + 8 * 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_once DISCUZ_ROOT.'./source/class/class_chinese.php';
- $c = new Chinese('utf8','gbk');
- $data = $c->Convert($data);
- return $data;
- }
- ?>
上面是夫唯老师提供给我的代码,因为我本身的服务器环境是utf-8的,所以我注释了2段代码,才显示正常.
第一段代码,是编码转换:
//$newhtml = convert_data($newhtml);
第二段代码,编码转换的方法
然后登陆论坛后台管理中心,工具->计划任务,增加一个新的计划任务名“输出html”,名字可以自己取,然后点击这个任务的“编辑”,在“任务脚本”里面输入:cron_makehtml_daily.php,其他更新时间设置,自己选择,也可以保持默认,点击“提交”.
然后,通过这个地址http://www.Vevb.com.cn/bbs/data/cache/newhtml.htm,可以获得html格式的论坛最新10个帖子,如果显示乱码,切换到UTF-8编码下查看.
在phpmps的模板文件需要调用的地方加入如下代码:
- <?php
- $data=array('foo'=>'bar');
- $data=http_build_query($data);
- $opts=array(
- 'http'=>array(
- 'method'=>'POST',
- 'header'=>"Content-type: application/x-www-form-urlencoded/r/n".
- "Content-Length: ".strlen($data)."/r/n",
- 'content'=>$data
- ),
- );
- $context=stream_context_create($opts);
- $html=file_get_contents('http://www.Vevb.com.cn/bbs/data/cache/newhtml.htm',false,$context);
- echo$html;
- ?>
OK,可以了,没调用出来的同学,清理下phpmps和discuz的缓存。
新闻热点
疑难解答