Home > Backend Development > PHP Tutorial > How to use php curl to simulate logging in to discuz and simulate posting

How to use php curl to simulate logging in to discuz and simulate posting

WBOY
Release: 2016-07-25 09:04:13
Original
1151 people have browsed it
  1. //link: http://bbs.it-home.org

  2. $discuz_url = 'http://127.0.0.1/discuz/';//Forum Address
  3. $login_url = $discuz_url .'logging.php?action=login';//Login page address

  4. $post_fields = array();

  5. //The following two items do not need to be modified
  6. $post_fields['loginfield'] = 'username';
  7. $post_fields['loginsubmit'] = 'true';
  8. //Username and password, must be filled in
  9. $post_fields['username'] = 'tianxin';
  10. $post_fields ['password'] = '111111';
  11. //Secure Question
  12. //link: http://bbs.it-home.org
  13. $post_fields['questionid'] = 0;
  14. $post_fields['answer'] = '';
  15. //@todo verification code
  16. $post_fields['seccoverify'] = '';

  17. //Get form 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 data, get COOKIE, and put the cookie file in the temp directory of the website Next

  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. //After getting the key cookie file, you can use the cookie file to simulate posting. The fid is the column ID of the forum

  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. //The hash code and login window here The regular hash code is different. The hidden here has an additional id attribute

  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. //Post title
  56. $post_data['subject'] = 'test2';
  57. //Post Content
  58. $post_data['message'] = 'test2';
  59. $post_data['topicsubmit'] = "yes";
  60. $post_data['extra'] = '';
  61. //Post tags
  62. $post_data['tags '] = 'test';
  63. //The hash code of the post, this is very critical! If this hash code is missing, discuz will warn you that the source page is incorrect
  64. $post_data['formhash']=$formhash;

  65. $ch = curl_init($send_url);

  66. curl_setopt( $ch, CURLOPT_REFERER, $send_url); //Disguise 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_se topt( $ch, CURLOPT_POST, 1);
  71. curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
  72. $contents = curl_exec($ch);
  73. curl_close($ch);

  74. //Clean up cookie file

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

Copy code

>>> For more articles about php simulated login, please refer to the topic link: php simulated login php curl simulation Complete login tutorial



source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template