Php simulates the implementation code of logging on to the discuz Forum through curl

Source: Internet
Author: User

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.
Php curl is really quite easy to use. articles related to searching on the Internet are about simulated curl login. Few people provide source code for simulating discuz posts. Copy codeThe Code is as follows: <? Php
$ Discuz_url = 'HTTP: // 127.0.0.1/discuz/'; // Forum address
$ Login_url = $ discuz_url. 'logging. 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 ('/<input \ s * type = "hidden" \ s * name = "formhash" \ s * value = "(.*?) "\ S * \/>/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 ('/<input \ s * type = "hidden" \ s * name = "formhash" \ s * id = "formhash" \ s * value = "(. *?) "\ S * \/>/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 );
?>

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.