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

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

admin11年前 (2015-05-11)技术文档1755
<?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以及模拟发帖” 的相关文章

如何自己手工获取自己的Access Token

如何自己手工获取自己的Access Token

在使用WP2PCS前,你需要了解,WP2PCS目前只支持百度网盘,(将来将支持360网盘和腾讯微云,这要看它们的API什么时候开放, 且容易开发)是基于百度PCS的API开发的,和坊间流传的抓取百度网盘文件直链不同,WP2PCS从百度网盘获取文件的源(二进制流),是百度许可并鼓 励开发者使用的接...

php 获取页面内容

function get_contents($url){     if(function_exists('file_get_contents')){       &nbs...

php登录函数login session+mysql

<?php // 为php和mysql剔除不安全html代码。 //http://blog.ddian.cn function safestrip($string){    $string = strip_tags(...

php 获取客户端的ip、地理信息、浏览器信息、本地真实ip

<?php  // 作用取得客户端的ip、地理信息、浏览器http://blog.qita.in  class get_gust_info {        ////获得访客浏...

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

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

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

推荐一些国内的Jquery CDN免费服务

Jquery是个非常流行的JS前端框架,在很多网站都能看到它的身影。很多网站都喜欢采用一些Jquery CDN加速服务,这样网站加载jquery会更快。之前火端网络的一些网站都是使用Google的jquery CDN,如:http://ajax.googleapis.com/ajax/lib...

发表评论

访客

看不清,换一张

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