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 );
?
?>