Php uses curl to simulate login to discuz and post

Source: Internet
Author: User
Php uses curl to simulate login to discuz and post simulation & lt ;? Php $ discuz_url127.0.0.1discuz; Forum address $ login_url $ discuz_url.logging.php? Actionlogin; logon page address ?? $ Post_fieldsarray (); php uses curl to simulate login to discuz and post

$ 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 ('/ /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 );

?

?>

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.