无限星辰工作室-客户无限互联网动力之源

标题: 模拟登录discuz!和发帖动作 [打印本页]

作者: crx349    时间: 2020-2-21 00:05
标题: 模拟登录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);

复制代码







欢迎光临 无限星辰工作室-客户无限互联网动力之源 (https://www.xmspace.net/) Powered by Discuz! X3.4