suchen
Heimphp教程PHP源码用php_curl对discuzx 2.5模拟登陆,保存cookie,并进行发帖的类

需要安装curl扩展

<?php
/*
*
* 作者:PHP中文网
* 类用途: 实现discuz2.5登陆发帖 
*/
class discuz_post{
 var $login_url;
 var $post_login_array=array( );
 var $post_url;
 var $cookie_file;
 public function get_formhash($login_url){
 $ch = curl_init();
 curl_setopt($ch, CURLOPT_URL, $login_url);
 curl_setopt($ch, CURLOPT_HEADER, false);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 $contents = curl_exec($ch);//print_r($contents);die;
curl_close($ch);
//正常发布的版本用如下方法可以获得登陆部分的formhash
 preg_match(&#39;/<input type="hidden"name="formhash"value="(.*)"/>/isU&#39;, $contents, $matches);
 //echo"<pre class="brush:php;toolbar:false">";
//print_r($matches);die;
 if(!empty($matches)) {
 $formhash = $matches[1];
 } else {
 // die(&#39;Not found the forumhash.&#39;);
}
 return $formhash;
}

 public function getcookie($login_url,$post){
 $cookie_file = tempnam(&#39;./temp&#39;,&#39;cookie&#39;);
//print_r($cookie_file);die;
 $ch = curl_init($login_url);
 curl_setopt($ch, CURLOPT_HEADER, false);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 curl_setopt($ch, CURLOPT_POST, true);
 curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
 curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
curl_exec($ch);
curl_close($ch);
$this->cookie_file=$cookie_file;
 return $cookie_file;

}

 public function use_cookie($send_url){
 $ch = curl_init();
 curl_setopt($ch, CURLOPT_URL, $send_url);
 curl_setopt($ch, CURLOPT_HEADER, false);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookie_file);
 $contents = curl_exec($ch);
curl_close($ch);
//获得发帖页面的fromhash
 preg_match_all(&#39;/<input type="hidden"name="formhash"id="formhash"value="(.*)"/>/isU&#39;,$contents,$matches);
if(!empty($matches)){
 $formhash = $matches[1][0];
 }else {
$formhash=&#39;&#39;;//没有
}
 return $formhash;

}

 public function post_newthread($send_url,$thread_data){
 $ch = curl_init();
 curl_setopt($ch, CURLOPT_URL, $send_url);
 curl_setopt($ch, CURLOPT_REFERER, $send_url);//伪装REFERER
 curl_setopt($ch, CURLOPT_HEADER, false);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookie_file);
 curl_setopt($ch, CURLOPT_POST, true);
 curl_setopt($ch, CURLOPT_POSTFIELDS, $thread_data);
 $contents = curl_exec($ch);
curl_close($ch);
 return 1;

}

}

//下面是代码例子*******************************************************************************
$rc= new discuz_post();
//登陆的地址
$login_url=&#39;http://bbs.phpchina.com/member.php?mod=logging&action=login&loginsubmit=yes&infloat=yes&#39;;

$theformhash= $rc->get_formhash($login_url);
//登陆部分需要发送的数据,这里处理没有验证码的
$login_array=array(
&#39;username&#39;=>&#39;用户名&#39;,
&#39;password&#39;=>&#39;密码&#39;,
&#39;referer&#39;=>&#39;http://bbs.phpchina.com/&#39;,
&#39;questionid&#39;=>0,
&#39;answer&#39;=>&#39;&#39;,
&#39;seccodeverify&#39;=>&#39;&#39;,
&#39;formhash&#39;=>$theformhash,//这个貌似没有也可以发帖滴
);
//获得cookie文件
$the_cookie_file= $rc->getcookie($login_url,$login_array);
$send_url =&#39;http://bbs.phpchina.com/forum.php?mod=post&action=newthread&fid=2&infloat=yes&#39;;
$thesendformhash= $rc->use_cookie($send_url);//利用cookie文件的
$post_array=array(
 &#39;subject&#39; =>"发帖,发帖测试",//标题
 &#39;message&#39; =>"内容噢噢噢噢噢噢噢噢",//要超过10个字,奶奶的
 &#39;topicsubmit&#39; =>"yes",
 &#39;extra&#39; => &#39;&#39;,
 &#39;tags&#39; => &#39;Curl&#39;,//帖子标签
&#39;formhash&#39;=>$thesendformhash,
);
$rc->post_newthread($send_url,$post_array);//发了一贴
echo"<pre class="brush:php;toolbar:false">";
print_r($rc);
echo $theformhash;
echo $the_cookie_file;
echo $thesendformhash;
unlink($rc->cookie_file);//删除cookie文件,也可以不删除
echo"ok!";
?>


<?php
/*
*
* 作者:PHP中文网
* 类用途: 实现discuz2.5登陆发帖 
*/
class discuz_post{
 var $login_url;
 var $post_login_array=array( );
 var $post_url;
 var $cookie_file;
 public function get_formhash($login_url){
 $ch = curl_init();
 curl_setopt($ch, CURLOPT_URL, $login_url);
 curl_setopt($ch, CURLOPT_HEADER, false);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 $contents = curl_exec($ch);//print_r($contents);die;
curl_close($ch);
//正常发布的版本用如下方法可以获得登陆部分的formhash
 preg_match(&#39;/<input type="hidden"name="formhash"value="(.*)"/>/isU&#39;, $contents, $matches);
 //echo"<pre class="brush:php;toolbar:false">";
//print_r($matches);die;
 if(!empty($matches)) {
 $formhash = $matches[1];
 } else {
 // die(&#39;Not found the forumhash.&#39;);
}
 return $formhash;
}

 public function getcookie($login_url,$post){
 $cookie_file = tempnam(&#39;./temp&#39;,&#39;cookie&#39;);
//print_r($cookie_file);die;
 $ch = curl_init($login_url);
 curl_setopt($ch, CURLOPT_HEADER, false);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 curl_setopt($ch, CURLOPT_POST, true);
 curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
 curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
curl_exec($ch);
curl_close($ch);
$this->cookie_file=$cookie_file;
 return $cookie_file;

}

 public function use_cookie($send_url){
 $ch = curl_init();
 curl_setopt($ch, CURLOPT_URL, $send_url);
 curl_setopt($ch, CURLOPT_HEADER, false);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookie_file);
 $contents = curl_exec($ch);
curl_close($ch);
//获得发帖页面的fromhash
 preg_match_all(&#39;/<input type="hidden"name="formhash"id="formhash"value="(.*)"/>/isU&#39;,$contents,$matches);
if(!empty($matches)){
 $formhash = $matches[1][0];
 }else {
$formhash=&#39;&#39;;//没有
}
 return $formhash;

}

 public function post_newthread($send_url,$thread_data){
 $ch = curl_init();
 curl_setopt($ch, CURLOPT_URL, $send_url);
 curl_setopt($ch, CURLOPT_REFERER, $send_url);//伪装REFERER
 curl_setopt($ch, CURLOPT_HEADER, false);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookie_file);
 curl_setopt($ch, CURLOPT_POST, true);
 curl_setopt($ch, CURLOPT_POSTFIELDS, $thread_data);
 $contents = curl_exec($ch);
curl_close($ch);
 return 1;

}

}

//下面是代码例子*******************************************************************************
$rc= new discuz_post();
//登陆的地址
$login_url=&#39;http://bbs.phpchina.com/member.php?mod=logging&action=login&loginsubmit=yes&infloat=yes&#39;;

$theformhash= $rc->get_formhash($login_url);
//登陆部分需要发送的数据,这里处理没有验证码的
$login_array=array(
&#39;username&#39;=>&#39;用户名&#39;,
&#39;password&#39;=>&#39;密码&#39;,
&#39;referer&#39;=>&#39;http://bbs.phpchina.com/&#39;,
&#39;questionid&#39;=>0,
&#39;answer&#39;=>&#39;&#39;,
&#39;seccodeverify&#39;=>&#39;&#39;,
&#39;formhash&#39;=>$theformhash,//这个貌似没有也可以发帖滴
);
//获得cookie文件
$the_cookie_file= $rc->getcookie($login_url,$login_array);
$send_url =&#39;http://bbs.phpchina.com/forum.php?mod=post&action=newthread&fid=2&infloat=yes&#39;;
$thesendformhash= $rc->use_cookie($send_url);//利用cookie文件的
$post_array=array(
 &#39;subject&#39; =>"发帖,发帖测试",//标题
 &#39;message&#39; =>"内容噢噢噢噢噢噢噢噢",//要超过10个字,奶奶的
 &#39;topicsubmit&#39; =>"yes",
 &#39;extra&#39; => &#39;&#39;,
 &#39;tags&#39; => &#39;Curl&#39;,//帖子标签
&#39;formhash&#39;=>$thesendformhash,
);
$rc->post_newthread($send_url,$post_array);//发了一贴
echo"<pre class="brush:php;toolbar:false">";
print_r($rc);
echo $theformhash;
echo $the_cookie_file;
echo $thesendformhash;
unlink($rc->cookie_file);//删除cookie文件,也可以不删除
echo"ok!";
?>



Stellungnahme
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn

Heiße KI -Werkzeuge

Undresser.AI Undress

Undresser.AI Undress

KI-gestützte App zum Erstellen realistischer Aktfotos

AI Clothes Remover

AI Clothes Remover

Online-KI-Tool zum Entfernen von Kleidung aus Fotos.

Undress AI Tool

Undress AI Tool

Ausziehbilder kostenlos

Clothoff.io

Clothoff.io

KI-Kleiderentferner

AI Hentai Generator

AI Hentai Generator

Erstellen Sie kostenlos Ai Hentai.

Heißer Artikel

R.E.P.O. Energiekristalle erklärten und was sie tun (gelber Kristall)
3 Wochen vorBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Beste grafische Einstellungen
3 Wochen vorBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. So reparieren Sie Audio, wenn Sie niemanden hören können
3 Wochen vorBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: Wie man alles in Myrise freischaltet
4 Wochen vorBy尊渡假赌尊渡假赌尊渡假赌

Heiße Werkzeuge

SublimeText3 Linux neue Version

SublimeText3 Linux neue Version

SublimeText3 Linux neueste Version

Dreamweaver Mac

Dreamweaver Mac

Visuelle Webentwicklungstools

Senden Sie Studio 13.0.1

Senden Sie Studio 13.0.1

Leistungsstarke integrierte PHP-Entwicklungsumgebung

mPDF

mPDF

mPDF ist eine PHP-Bibliothek, die PDF-Dateien aus UTF-8-codiertem HTML generieren kann. Der ursprüngliche Autor, Ian Back, hat mPDF geschrieben, um PDF-Dateien „on the fly“ von seiner Website auszugeben und verschiedene Sprachen zu verarbeiten. Es ist langsamer und erzeugt bei der Verwendung von Unicode-Schriftarten größere Dateien als Originalskripte wie HTML2FPDF, unterstützt aber CSS-Stile usw. und verfügt über viele Verbesserungen. Unterstützt fast alle Sprachen, einschließlich RTL (Arabisch und Hebräisch) und CJK (Chinesisch, Japanisch und Koreanisch). Unterstützt verschachtelte Elemente auf Blockebene (wie P, DIV),

VSCode Windows 64-Bit-Download

VSCode Windows 64-Bit-Download

Ein kostenloser und leistungsstarker IDE-Editor von Microsoft