本貼介紹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.49028c.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.49028c.com.cn/bbs/data/cache/newhtml.htm',false,$context);
- echo$html;
- ?>
OK,可以了,沒調用出來的同學,清理下phpmps和discuz的緩存。
新聞熱點
疑難解答