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

php中用curl模拟登录discuz以及模拟发帖

发布:smiling 来源: PHP粉丝网  添加日期:2014-01-06 10:20:36 浏览: 评论:0 

本文章完美的利用了php的curl功能实现模拟登录discuz以及模拟发帖,本教程供参考学习.

  1. <?php 
  2. $discuz_url = ‘http://localhost/klive/root/Discuz_X2.5_SC_GBK/’;//论坛地址 
  3. $login_url = $discuz_url .’member.php?mod=logging&action=login’;//登录页地址 
  4. $post_fields = array(); 
  5. //以下两项不需要修改 
  6. $post_fields['loginfield'] = ‘username’; 
  7. $post_fields['loginsubmit'] = ‘true’; 
  8. //用户名和密码,必须填写 
  9. $post_fields['username'] = ‘admin’; 
  10. $post_fields['password'] = ‘admin’; 
  11. //安全提问 
  12. $post_fields['questionid'] = 0; 
  13. $post_fields['answer'] = ”; 
  14. //@todo验证码 
  15. $post_fields['seccodeverify'] = ”; 
  16. //获取表单FORMHASH 
  17. $ch = curl_init($login_url); 
  18. curl_setopt($ch, CURLOPT_HEADER, 0); 
  19. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
  20. $contents = curl_exec($ch); 
  21. curl_close($ch); 
  22. preg_match(‘/<inputs*type=”hidden”s*name=”formhash”s*value=”(.*?)”s*/>/i’, $contents
  23. $matches); 
  24. if(!emptyempty($matches)) { 
  25. $formhash = $matches[1]; 
  26. else { 
  27. die(‘Not found the forumhash.’); 
  28. //POST数据,获取COOKIE,cookie文件放在网站的temp目录下 
  29. $cookie_file = tempnam(‘./temp’,'cookie’); 
  30. $ch = curl_init($login_url); 
  31. curl_setopt($ch, CURLOPT_HEADER, 0); 
  32. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
  33. curl_setopt($ch, CURLOPT_POST, 1); 
  34. curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields); 
  35. curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file); 
  36. curl_exec($ch); 
  37. curl_close($ch); 
  38. //取到了关键的cookie文件就可以带着cookie文件去模拟发帖,fid为论坛的栏目ID 
  39. $send_url = $discuz_url.”forum.php?mod=post&action=newthread&fid=2″; 
  40. $ch = curl_init($send_url); 
  41. curl_setopt($ch, CURLOPT_HEADER, 0); 
  42. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
  43. curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file); 
  44. $contents = curl_exec($ch); 
  45. curl_close($ch); 
  46. //这里的hash码和登陆窗口的hash码的正则不太一样,这里的hidden多了一个id属性 
  47. preg_match(‘/<inputs*type=”hidden”s*name=”formhash”s*id=”formhash”s*value=”(.*?)”s*/>/i 
  48. ’, $contents$matches); 
  49. if(!emptyempty($matches)) { 
  50. $formhash = $matches[1]; 
  51. else { 
  52. die(‘Not found the forumhash.’); 
  53. $post_data = array(); 
  54. //帖子标题 
  55. $post_data['subject'] = ‘test2′; 
  56. //帖子内容 
  57. $post_data['message'] = ‘test2′; 
  58. $post_data['topicsubmit'] = “yes”; 
  59. $post_data['extra'] = ”; 
  60. //帖子标签 
  61. $post_data['tags'] = ‘test’; 
  62. //帖子的hash码,这个非常关键!假如缺少这个hash码,discuz会警告你来路的页面不正确 
  63. $post_data['formhash']=$formhash
  64. $ch = curl_init($send_url); 
  65. curl_setopt($ch, CURLOPT_REFERER, $send_url);       //伪装REFERER 
  66. curl_setopt($ch, CURLOPT_HEADER, 0); 
  67. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0); 
  68. curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file); 
  69. curl_setopt($ch, CURLOPT_POST, 1); 
  70. curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); 
  71. $contents = curl_exec($ch); 
  72. curl_close($ch); 
  73. //清理cookie文件 
  74. unlink($cookie_file); 
  75. ?> 

Tags: curl 模拟登录 discuz 模拟发帖

分享到: