当前位置:Gxlcms > PHP教程 > php通过curl模拟登陆DZ论坛_PHP

php通过curl模拟登陆DZ论坛_PHP

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

libcurl同时也支持HTTPS认证、HTTP POST、HTTP PUT、 FTP 上传(这个也能通过PHP的FTP扩展完成)、HTTP 基于表单的上传、代理、cookies和用户名+密码的认证。

  1. <?php
  2. $discuz_url = 'http://www.bitsCN.com/';//论坛地址
  3. $login_url = $discuz_url .'login.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. $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('/<input\s*type="hidden"\s*name="formhash"\s*value="(.*?)"\s*\>/i', $contents, $matches);
  23. if(!empty($matches)) {
  24. $formhash = $matches[1];
  25. } else {
  26. die('Not found the forumhash.');
  27. }
  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."post.php?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('/<input\s*type="hidden"\s*name="formhash"\s*id="formhash"\s*value="(.*?)"\s*\>/i', $contents, $matches);
  48. if(!empty($matches)) {
  49. $formhash = $matches[1];
  50. } else {
  51. die('Not found the forumhash.');
  52. }
  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. ?>
  76. </input\s*type="hidden"\s*name="formhash"\s*id="formhash"\s*value="(.*?)"\s*\></input\s*type="hidden"\s*name="formhash"\s*value="(.*?)"\s*\>

以上所述就是本文的全部内容了,希望大家能够喜欢。

人气教程排行