php中用curl模拟登录discuz以及模拟发帖
本文章完美的利用了php的curl功能实现模拟登录discuz以及模拟发帖,本教程供参考学习.
- <?php
- $discuz_url = ‘http:
- $login_url = $discuz_url .’member.php?mod=logging&action=login’;
- $post_fields = array();
-
- $post_fields['loginfield'] = ‘username’;
- $post_fields['loginsubmit'] = ‘true’;
-
- $post_fields['username'] = ‘admin’;
- $post_fields['password'] = ‘admin’;
-
- $post_fields['questionid'] = 0;
- $post_fields['answer'] = ”;
-
- $post_fields['seccodeverify'] = ”;
-
- $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(‘/<inputs*type=”hidden”s*name=”formhash”s*value=”(.*?)”s*/>/i’, $contents,
- $matches);
- if(!emptyempty($matches)) {
- $formhash = $matches[1];
- } else {
- die(‘Not found the forumhash.’);
- }
-
- $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);
-
- $send_url = $discuz_url.”forum.php?mod=post&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);
-
- preg_match(‘/<inputs*type=”hidden”s*name=”formhash”s*id=”formhash”s*value=”(.*?)”s*/>/i
- ’, $contents, $matches);
- if(!emptyempty($matches)) {
- $formhash = $matches[1];
- } else {
- die(‘Not found the forumhash.’);
- }
- $post_data = array();
-
- $post_data['subject'] = ‘test2′;
-
- $post_data['message'] = ‘test2′;
- $post_data['topicsubmit'] = “yes”;
- $post_data['extra'] = ”;
-
- $post_data['tags'] = ‘test’;
-
- $post_data['formhash']=$formhash;
- $ch = curl_init($send_url);
- curl_setopt($ch, CURLOPT_REFERER, $send_url);
- 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);
-
- unlink($cookie_file);
- ?>