php through curl simulation login discuz forum implementation code

  • 2020-05-16 06:25:48
  • OfStack

libcurl also supports HTTPS authentication, HTTP POST, HTTP PUT, FTP uploads (which can also be done with PHP's FTP extension), HTTP form-based uploads, proxy, cookies, and username + password authentication.
php curl is really quite easy to use, online 1 search related articles are about curl simulated login, very few people provide simulated discuz posted source code.
 
<?php 
$discuz_url = 'http://127.0.0.1/discuz/';// BBS address  
$login_url = $discuz_url .'logging.php?action=login';// Login page address  

$post_fields = array(); 
// The following two items do not need to be modified  
$post_fields['loginfield'] = 'username'; 
$post_fields['loginsubmit'] = 'true'; 
// User name and password, must be filled out  
$post_fields['username'] = 'tianxin'; 
$post_fields['password'] = '111111'; 
// Security question  
$post_fields['questionid'] = 0; 
$post_fields['answer'] = ''; 
//@todo Verification code  
$post_fields['seccodeverify'] = ''; 
// Get the form FORMHASH 
$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 Data, access COOKIE,cookie The file is on the website temp directory  
$cookie_file = tempnam('./temp','cookie'); 
$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); 
// You get the key cookie You can bring it with you cookie File to simulate Posting ,fid For the forum column ID 
$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); 
// Here, hash Code and login window hash The code is not regular enough 1 B: yes, here it is hidden Much more 1 a 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['subject'] = 'test2'; 
// Post content  
$post_data['message'] = 'test2'; 
$post_data['topicsubmit'] = "yes"; 
$post_data['extra'] = ''; 
// Post the label  
$post_data['tags'] = 'test'; 
// post hash Code, this is critical! If you don't have this hash Code, discuz It will warn you that the page you came from is incorrect  
$post_data['formhash']=$formhash; 

$ch = curl_init($send_url); 
curl_setopt($ch, CURLOPT_REFERER, $send_url); // camouflage 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); 
// Clean up the cookie file  
unlink($cookie_file); 
?> 

Related articles: