找回密码
 立即注册

QQ登录

只需一步,快速开始

模拟登录discuz!和发帖动作

一个模拟发帖例子:(未测试Discuz!x3.4兼容性)
  1. $discuz_url = 'http://127.0.0.1/discuz/';//论坛地址
  2. $login_url =$discuz_url . 'member.php?mod=logging&action=login'; //登录页地址
  3. $post_fields = array();
  4. //以下两项不需要修改
  5. $post_fields['loginfield'] = 'username';
  6. $post_fields['loginsubmit'] = 'true';
  7. //用户名和密码,必须填写
  8. $post_fields['username'] = 'tianxin';
  9. $post_fields['password'] = '111111';
  10. //安全提问
  11. $post_fields['questionid'] = 0;
  12. $post_fields['answer'] = '';
  13. //@todo验证码
  14. $post_fields['seccodeverify'] = '';

  15. //获取表单FORMHASH
  16. $ch = curl_init($login_url);
  17. curl_setopt($ch, CURLOPT_HEADER, 0);
  18. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  19. $contents = curl_exec($ch);
  20. curl_close($ch);
  21. preg_match('/<input\s*type="hidden"\s*name="formhash"\s*value="(.*?)"\s*\/>/i', $contents, $matches);
  22. if(!empty($matches)) {
  23. &#160;&#160;&#160;&#160;$formhash = $matches[1];
  24. } else {
  25. &#160;&#160;&#160;&#160;die('Not found the forumhash.');
  26. }

  27. //POST数据,获取COOKIE,cookie文件放在网站的temp目录下
  28. $cookie_file = tempnam('./temp','cookie');

  29. $ch = curl_init($login_url);
  30. curl_setopt($ch, CURLOPT_HEADER, 0);
  31. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  32. curl_setopt($ch, CURLOPT_POST, 1);
  33. curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
  34. curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
  35. curl_exec($ch);
  36. curl_close($ch);

  37. //取到了关键的cookie文件就可以带着cookie文件去模拟发帖,fid为论坛的栏目ID
  38. $send_url = $discuz_url."post.php?action=newthread&fid=2";

  39. $ch = curl_init($send_url);
  40. curl_setopt($ch, CURLOPT_HEADER, 0);
  41. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  42. curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
  43. $contents = curl_exec($ch);
  44. curl_close($ch);

  45. //这里的hash码和登陆窗口的hash码的正则不太一样,这里的hidden多了一个id属性
  46. preg_match('/<input\s*type="hidden"\s*name="formhash"\s*id="formhash"\s*value="(.*?)"\s*\/>/i', $contents, $matches);
  47. if(!empty($matches)) {
  48. &#160;&#160;&#160;&#160;$formhash = $matches[1];
  49. } else {
  50. &#160;&#160;&#160;&#160;die('Not found the forumhash.');
  51. }

  52. $post_data = array();
  53. //帖子标题
  54. $post_data['subject'] = 'test2';
  55. //帖子内容
  56. $post_data['message'] = 'test2';
  57. $post_data['topicsubmit'] = "yes";
  58. $post_data['extra'] = '';
  59. //帖子标签
  60. $post_data['tags'] = 'test';
  61. //帖子的hash码,这个非常关键!假如缺少这个hash码,discuz会警告你来路的页面不正确
  62. $post_data['formhash']=$formhash;

  63. $ch = curl_init($send_url);
  64. curl_setopt($ch, CURLOPT_REFERER, $send_url);&#160;&#160;&#160;&#160;&#160;&#160;&#160;//伪装REFERER
  65. curl_setopt($ch, CURLOPT_HEADER, 0);
  66. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
  67. curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
  68. curl_setopt($ch, CURLOPT_POST, 1);
  69. curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
  70. $contents = curl_exec($ch);
  71. curl_close($ch);

  72. //清理cookie文件
  73. unlink($cookie_file);

复制代码



本教程由无限星辰工作室CRX349独家整理和提供,转载请注明地址,谢谢。本文地址:https://www.xmspace.net/thread-812-1-1.html
无限星辰工作室  好集导航 Discuz全集下载  星辰站长网  集热爱361  一品文学  手机小游戏合集   海外空间网 星辰api  星辰支付二维码管理平台 阿里云服务器 腾讯云服务器
服务Discuz!建站|DiscuzQ配置|二开|小程序|APP|搬家|挂马清理|防护|Win/Linux环境搭建|优化|运维|
服务理念:专业 诚信 友好QQ842062626 服务项目 Q群315524225

发表于 2020-2-21 00:05:49 | 显示全部楼层 |阅读模式

回复 | 使用道具 举报

该帖共收到 0 条回复!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

美图秀

    • fastadmin 后台界面使用字段数组类型
    • Discuz!x3.5 修改标题高亮颜色
    • Discuz!x3.5 应用中心 下载应用一直下载中
    • 帖子定时显示
    • 论坛辅助审核
拖动客服框
Online Service
点击这里给我发消息
点击这里联系我们
微信扫一扫
在线客服
快速回复 返回顶部 返回列表