当前位置:Gxlcms > PHP教程 > phpcurl模拟登录discuz并模拟发帖的实现方法

phpcurl模拟登录discuz并模拟发帖的实现方法

时间:2021-07-01 10:21:17 帮助过:16人阅读

  1. //link:http://bbs.it-home.org

  2. $discuz_url = 'http://127.0.0.1/discuz/';//论坛地址
  3. $login_url = $discuz_url .'logging.php?action=login';//登录页地址

  4. $post_fields = array();

  5. //以下两项不需要修改
  6. $post_fields['loginfield'] = 'username';
  7. $post_fields['loginsubmit'] = 'true';
  8. //用户名和密码,必须填写
  9. $post_fields['username'] = 'tianxin';
  10. $post_fields['password'] = '111111';
  11. //安全提问
  12. //link: http://bbs.it-home.org
  13. $post_fields['questionid'] = 0;
  14. $post_fields['answer'] = '';
  15. //@todo验证码
  16. $post_fields['seccodeverify'] = '';

  17. //获取表单FORMHASH

  18. $ch = curl_init($login_url);
  19. curl_setopt($ch, CURLOPT_HEADER, 0);
  20. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  21. $contents = curl_exec($ch);
  22. curl_close($ch);
  23. preg_match('//i', $contents, $matches);
  24. if(!empty($matches)) {
  25. $formhash = $matches[1];
  26. } else {
  27. die('Not found the forumhash.');
  28. }

  29. //POST数据,获取COOKIE,cookie文件放在网站的temp目录下

  30. $cookie_file = tempnam('./temp','cookie');

  31. $ch = curl_init($login_url);

  32. curl_setopt($ch, CURLOPT_HEADER, 0);
  33. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  34. curl_setopt($ch, CURLOPT_POST, 1);
  35. curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
  36. curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
  37. curl_exec($ch);
  38. curl_close($ch);

  39. //取到了关键的cookie文件就可以带着cookie文件去模拟发帖,fid为论坛的栏目ID

  40. $send_url = $discuz_url."post.php?action=newthread&fid=2";

  41. $ch = curl_init($send_url);

  42. curl_setopt($ch, CURLOPT_HEADER, 0);
  43. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  44. curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
  45. $contents = curl_exec($ch);
  46. curl_close($ch);

  47. //这里的hash码和登陆窗口的hash码的正则不太一样,这里的hidden多了一个id属性

  48. preg_match('//i', $contents, $matches);
  49. if(!empty($matches)) {
  50. $formhash = $matches[1];
  51. } else {
  52. die('Not found the forumhash.');
  53. }

  54. $post_data = array();

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

  65. $ch = curl_init($send_url);

  66. curl_setopt($ch, CURLOPT_REFERER, $send_url); //伪装REFERER
  67. curl_setopt($ch, CURLOPT_HEADER, 0);
  68. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
  69. curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
  70. curl_setopt($ch, CURLOPT_POST, 1);
  71. curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
  72. $contents = curl_exec($ch);
  73. curl_close($ch);

  74. //清理cookie文件

  75. unlink($cookie_file);
  76. ?>

>>> 更多有关php模拟登录的文章,请参考专题链接:php模拟登录 php curl模拟登录教程大全

人气教程排行