当前位置:首页 > CMS教程 > Discuz > 列表

phpmps调用discuz x方法

发布:smiling 来源: PHP粉丝网  添加日期:2014-04-01 22:14:34 浏览: 评论:0 

本贴介绍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/目录中.

  1. <?php 
  2. if(!defined('IN_DISCUZ')) { 
  3.  
  4. exit('Access Denied'); 
  5.  
  6. } 
  7.  
  8. $newhtml = ''; 
  9.  
  10. $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 
  11.  
  12. while($new = DB::fetch($query)) { 
  13.  
  14. $newsubject = cutstr($new['subject'],48); 
  15.  
  16. $newurl = 'http://www.100de.com.cn/bbs/thread-'.$new['tid'].'-1-1.html'; 
  17.  
  18. $newhtml .= '<li><a target="_blank" href="'.$newurl.'">'.$newsubject.'</a></li>'; 
  19.  
  20. }</p><p>$newhtml = convert_data($newhtml); 
  21.  
  22. writehtml('newhtml.htm', $newhtml); 
  23.  
  24. //写入html文件 
  25.  
  26. function writehtml($file, $html) { 
  27.  
  28.         global $timestamp; 
  29.  
  30.         $yearmonth = gmdate('Ym', $timestamp + 8 * 3600); 
  31.  
  32.         $logdir = DISCUZ_ROOT.'./data/cache/'; 
  33.  
  34.         $logfile = $logdir.$file; 
  35.  
  36.         if($fp = @fopen($logfile, 'w')) { 
  37.  
  38.                 @flock($fp, 2); 
  39.  
  40.                 fwrite($fp, "$html"); 
  41.  
  42.                 fclose($fp); 
  43.  
  44.         } 
  45.  
  46. } 
  47.  
  48. function convert_data($data) { 
  49.  
  50.         include_once DISCUZ_ROOT.'./source/class/class_chinese.php'; 
  51.  
  52.         $c = new Chinese('utf8','gbk'); 
  53.  
  54.         $data = $c->Convert($data); 
  55.  
  56.         return $data; 
  57.  
  58. } 
  59. ?> 

上面是夫唯老师提供给我的代码,因为我本身的服务器环境是utf-8的,所以我注释了2段代码,才显示正常.

第一段代码,是编码转换:

//$newhtml = convert_data($newhtml);

第二段代码,编码转换的方法

然后登陆论坛后台管理中心,工具->计划任务,增加一个新的计划任务名“输出html”,名字可以自己取,然后点击这个任务的“编辑”,在“任务脚本”里面输入:cron_makehtml_daily.php,其他更新时间设置,自己选择,也可以保持默认,点击“提交”.

然后,通过这个地址http://www.phpfensi.com.cn/bbs/data/cache/newhtml.htm,可以获得html格式的论坛最新10个帖子,如果显示乱码,切换到UTF-8编码下查看.

在phpmps的模板文件需要调用的地方加入如下代码:

  1. <?php 
  2.  
  3.   $data=array('foo'=>'bar'); 
  4.  
  5.   $data=http_build_query($data); 
  6.  
  7.   $opts=array( 
  8.  
  9.     'http'=>array( 
  10.  
  11.    'method'=>'POST', 
  12.  
  13.    'header'=>"Content-type: application/x-www-form-urlencoded\r\n". 
  14.  
  15.     "Content-Length: ".strlen($data)."\r\n", 
  16.  
  17.   'content'=>$data 
  18.  
  19.   ), 
  20.  
  21.   ); 
  22.  
  23. $context=stream_context_create($opts); 
  24.  
  25. $html=file_get_contents('http://www.phpfensi.com.cn/bbs/data/cache/newhtml.htm',false,$context); 
  26.  
  27. echo$html; 
  28.  
  29. ?> 

OK,可以了,没调用出来的同学,清理下phpmps和discuz的缓存。

Tags: phpmps 调用discuz x

分享到: