Php simulates login to DZ Forum through curl _ PHP Tutorial

Source: Internet
Author: User
Php simulates login to the DZ Forum through curl. Php simulates login to the DZ Forum through curl. This article will introduce you to PhpCURL to simulate login to the Forum and collect data instances, if you are interested in using the curl simulated login function, you can log on to the DZ Forum using the curl simulated login function in php.

This article will introduce you to the Php CURL simulated login to the Forum and collect data instances. if you are interested in using the curl simulated login function, go to the reference page.

Libcurl also supports HTTPS authentication, http post, http put, and FTP upload (this can also be completed through the FTP extension of PHP) HTTP form-based Upload, proxy, cookies, user name + password authentication.

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

$ Discuz_url = 'http: // www.jb51.net/'#//forum address

$ Login_url = $ discuz_url. 'login. php? Action = login '; // logon page address

$ Post_fields = array ();

// The following two items do not need to be modified

$ Post_fields ['loginfield'] = 'username ';

$ Post_fields ['loginsubmit '] = 'true ';

// Username and password, required

$ Post_fields ['username'] = 'tiancei ';

$ Post_fields ['password'] = '000000 ';

// Security question

$ Post_fields ['questionid'] = 0;

$ Post_fields ['answer'] = '';

// @ Todo verification code

$ Post_fields ['seccodeverify '] = '';

// Obtain the FORMHASH form

$ Ch = curl_init ($ login_url );

Curl_setopt ($ ch, CURLOPT_HEADER, 0 );

Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 );

$ Contents = curl_exec ($ ch );

Curl_close ($ ch );

Preg_match ('/ /I ', $ contents, $ matches );

If (! Empty ($ matches )){

$ Formhash = $ matches [1];

} Else {

Die ('not found the forumhash .');

}

// POST the data to obtain the COOKIE. the cookie file is stored in the temp directory of the website.

$ Cookie_file = tempnam ('./temp', 'cooker ');

$ Ch = curl_init ($ login_url );

Curl_setopt ($ ch, CURLOPT_HEADER, 0 );

Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 );

Curl_setopt ($ ch, CURLOPT_POST, 1 );

Curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ post_fields );

Curl_setopt ($ ch, CURLOPT_COOKIEJAR, $ cookie_file );

Curl_exec ($ ch );

Curl_close ($ ch );

// Take the key cookie file to simulate post with the cookie file. fid is the topic ID of the Forum.

$ Send_url = $ discuz_url. "post. php? Action = newthread & fid = 2 ";

$ Ch = curl_init ($ send_url );

Curl_setopt ($ ch, CURLOPT_HEADER, 0 );

Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 );

Curl_setopt ($ ch, CURLOPT_COOKIEFILE, $ cookie_file );

$ Contents = curl_exec ($ ch );

Curl_close ($ ch );

// The hash code here is not the same as the hash code in the login window. the hidden here has an id attribute.

Preg_match ('/ /I ', $ contents, $ matches );

If (! Empty ($ matches )){

$ Formhash = $ matches [1];

} Else {

Die ('not found the forumhash .');

}

$ Post_data = array ();

// Post title

$ Post_data ['subobject'] = 'test2 ';

// Post content

$ Post_data ['message'] = 'test2 ';

$ Post_data ['topicsubmit '] = "yes ";

$ Post_data ['Extra '] = '';

// Post tag

$ Post_data ['tags'] = 'test ';

// The Post hash code, which is critical! If this hash code is missing, discuz will warn you of incorrect webpage

$ Post_data ['formhash'] = $ formhash;

$ Ch = curl_init ($ send_url );

Curl_setopt ($ ch, CURLOPT_REFERER, $ send_url); // disguise REFERER

Curl_setopt ($ ch, CURLOPT_HEADER, 0 );

Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 0 );

Curl_setopt ($ ch, CURLOPT_COOKIEFILE, $ cookie_file );

Curl_setopt ($ ch, CURLOPT_POST, 1 );

Curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ post_data );

$ Contents = curl_exec ($ ch );

Curl_close ($ ch );

// Clear cookie files

Unlink ($ cookie_file );

?>

The above is all the content of this article. I hope you will like it.

In this article, I will introduce you to the Php CURL simulated login to the Forum and collect data instances. if you are interested in using the curl simulated login function, please go...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.