php中用curl模拟登录discuz以及模拟发帖
发布:smiling 来源: PHP粉丝网 添加日期:2014-01-06 10:20:36 浏览: 评论:0
本文章完美的利用了php的curl功能实现模拟登录discuz以及模拟发帖,本教程供参考学习.
- <?php
- $discuz_url = ‘http://localhost/klive/root/Discuz_X2.5_SC_GBK/’;//论坛地址
- $login_url = $discuz_url .’member.php?mod=logging&action=login’;//登录页地址
- $post_fields = array();
- //以下两项不需要修改
- $post_fields['loginfield'] = ‘username’;
- $post_fields['loginsubmit'] = ‘true’;
- //用户名和密码,必须填写
- $post_fields['username'] = ‘admin’;
- $post_fields['password'] = ‘admin’;
- //安全提问
- $post_fields['questionid'] = 0;
- $post_fields['answer'] = ”;
- //@todo验证码
- $post_fields['seccodeverify'] = ”;
- //获取表单FORMHASH
- $ch = curl_init($login_url);
- curl_setopt($ch, CURLOPT_HEADER, 0);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- $contents = curl_exec($ch);
- curl_close($ch);
- preg_match(‘/<inputs*type=”hidden”s*name=”formhash”s*value=”(.*?)”s*/>/i’, $contents,
- $matches);
- if(!emptyempty($matches)) {
- $formhash = $matches[1];
- } else {
- die(‘Not found the forumhash.’);
- }
- //POST数据,获取COOKIE,cookie文件放在网站的temp目录下
- $cookie_file = tempnam(‘./temp’,'cookie’);
- $ch = curl_init($login_url);
- curl_setopt($ch, CURLOPT_HEADER, 0);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_POST, 1);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
- curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
- curl_exec($ch);
- curl_close($ch);
- //取到了关键的cookie文件就可以带着cookie文件去模拟发帖,fid为论坛的栏目ID
- $send_url = $discuz_url.”forum.php?mod=post&action=newthread&fid=2″;
- $ch = curl_init($send_url);
- curl_setopt($ch, CURLOPT_HEADER, 0);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
- $contents = curl_exec($ch);
- curl_close($ch);
- //这里的hash码和登陆窗口的hash码的正则不太一样,这里的hidden多了一个id属性
- preg_match(‘/<inputs*type=”hidden”s*name=”formhash”s*id=”formhash”s*value=”(.*?)”s*/>/i
- ’, $contents, $matches);
- if(!emptyempty($matches)) {
- $formhash = $matches[1];
- } else {
- die(‘Not found the forumhash.’);
- }
- $post_data = array();
- //帖子标题
- $post_data['subject'] = ‘test2′;
- //帖子内容
- $post_data['message'] = ‘test2′;
- $post_data['topicsubmit'] = “yes”;
- $post_data['extra'] = ”;
- //帖子标签
- $post_data['tags'] = ‘test’;
- //帖子的hash码,这个非常关键!假如缺少这个hash码,discuz会警告你来路的页面不正确
- $post_data['formhash']=$formhash;
- $ch = curl_init($send_url);
- curl_setopt($ch, CURLOPT_REFERER, $send_url); //伪装REFERER
- curl_setopt($ch, CURLOPT_HEADER, 0);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
- curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
- curl_setopt($ch, CURLOPT_POST, 1);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
- $contents = curl_exec($ch);
- curl_close($ch);
- //清理cookie文件
- unlink($cookie_file);
- ?>
Tags: curl 模拟登录 discuz 模拟发帖
相关文章
- ·discuz模拟登录实现自动顶帖php程序(2014-08-27)
- ·Discuz3.1论坛升级问题集(2013-11-15)
- ·升级Discuz3.1时update.php访问出错(2013-11-15)
- ·discuz论坛项目总结(2013-11-15)
- ·怎么让discuz网站版块下面显示版块简介(2013-11-15)
- ·导致Discuz!论坛出现空白页面的一些原因及解决方法简要分析(2013-11-15)
- ·百度discuz结构化数据插件上线 收录有望(2013-11-15)
- ·discuz email 已经被注册的解决办法(2013-11-29)
- ·discuz X3全局变量$_G(2014-01-10)
- ·discuz模板制作过程中的几点总结(2014-01-10)
- ·最新Discuz防灌水的方法和防灌水插件的推荐(2014-01-10)
- ·Discuz论坛 创始人/超级管理员密码忘记解决办法(2014-01-10)
- ·discuz发私信,GBK格式英文或者数字可以显示,中文不显示(2014-03-31)
- ·discuz和jquery冲突暂行解决办法(2014-03-31)
- ·Discuz X3 mysql数据库主从同步 读写分离(2014-03-31)
- ·有关discuz在修改过程中的一些配置以及文件位置(2014-03-31)
推荐文章
热门文章
最新评论文章
- 写给考虑创业的年轻程序员(10)
- PHP新手上路(一)(7)
- 惹恼程序员的十件事(5)
- PHP邮件发送例子,已测试成功(5)
- 致初学者:PHP比ASP优秀的七个理由(4)
- PHP会被淘汰吗?(4)
- PHP新手上路(四)(4)
- 如何去学习PHP?(2)
- 简单入门级php分页代码(2)
- php中邮箱email 电话等格式的验证(2)