当前位置:首页 > 技术文档 > 正文内容

php 使用curl模拟登录discuz以及模拟发帖

admin11年前 (2015-05-11)技术文档1758
<?php
$discuz_url = 'http://127.0.0.1/discuz/';//论坛地址
$login_url = $discuz_url .'logging.php?action=login';//登录页地址
 
 
$post_fields = array();
//以下两项不需要修改
$post_fields['loginfield'] = 'username';
$post_fields['loginsubmit'] = 'true';
//用户名和密码,必须填写
$post_fields['username'] = 'tianxin';
$post_fields['password'] = '111111';
//安全提问
$post_fields['questionid'] = 0;
$post_fields['answer'] = '';
//@todo验证码
$post_fields['seccodeverify'] = '';
 
//获取表单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数据,获取COOKIE,cookie文件放在网站的temp目录下
$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);
 
//取到了关键的cookie文件就可以带着cookie文件去模拟发帖,fid为论坛的栏目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);
 
//这里的hash码和登陆窗口的hash码的正则不太一样,这里的hidden多了一个id属性
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_data['subject'] = 'test2';
//帖子内容
$post_data['message'] = 'test2';
$post_data['topicsubmit'] = "yes";
$post_data['extra'] = '';
//帖子标签
$post_data['tags'] = 'test';
//帖子的hash码,这个非常关键!假如缺少这个hash码,discuz会警告你来路的页面不正确
$post_data['formhash']=$formhash;
 
 
$ch = curl_init($send_url);
curl_setopt($ch, CURLOPT_REFERER, $send_url);       //伪装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);
 
//清理cookie文件http://blog.ddian.cn
unlink($cookie_file);
 
?>


分享到:

扫描二维码推送至手机访问。

版权声明:本文由云河空间发布,如需转载请注明出处。

本文链接:http://yuyunhe.cn/index.php/post/33.html

分享给朋友:

“php 使用curl模拟登录discuz以及模拟发帖” 的相关文章

php 判断手机访问

//手机来访 function is_mobile() {     $user_agent = $_SERVER['HTTP_USER_AGENT'];    &n...

dz中“QQ互联”出现Discuz! Database Error解决办法

dz中“QQ互联”出现Discuz! Database Error解决办法

dz中QQ登陆出现Discuz! Database Error解决办法dz站长朋友相信肯定有人遇到做的QQ互联登录,根本没法用,这让网站的社交登录性能大打折扣,这么解决?下面就看大神的神操作!...

用php gettext库来开发多语言系统

用php gettext库来开发多语言系统

通常人们写程序时都是将文字写死在程序里的, 比如:echo "Hello World!";  ,假如要改成它国语言,写国际化程序,就要逐个打开进行修改,程序较短时还行,若程序有上万甚至更多,改起来就不是那么容易了。近来随着i18n的逐渐标 准化,我也来讲一讲在PHP中如...

php二维数组转换为一维数组的几种方法

在开发过程中,我们经常需要将二维数组转为一维数组,个人总结了2种方法,分享给大家如何将下面的二维数组转为一维数组。复制代码 代码如下:$msg = array(  array(    'id'=>'45',    'name'=>'...

CentOS minimal版安装图形界面的步骤

1、连接网络:CentOS minimal.iso安装好后,进入终端,默认是不开网络的,首先启用网卡, 自动获取ip.ifconfig eth0 up   www.2cto.com dhclient eth0这时候再 ifconfig 一下,就可以看见已经自动分配好IP...

LNMP一键安装包安装以及常用的SSH命令使用整理

LNMP一键安装包安装以及常用的SSH命令使用整理

如果我们使用Linux VPS主机肯定会有接触到一键安装包,比如我们接触较多的就是LAMP、LNMP、LLSMP等。麦子翻看以前的文章毕竟分享的虚拟主机较多一些,VPS 主机类的教程相对少很多(基本没有),以后在分享到一些VPS主机优惠的时候穿擦会有从基础到提升的VPS教程,一来是自己学习的记...

发表评论

访客

看不清,换一张

◎欢迎参与讨论,请在这里发表您的看法和观点。