php通过curl模拟登陆DZ论坛


Posted in PHP onMay 11, 2015

libcurl同时也支持HTTPS认证、HTTP POST、HTTP PUT、 FTP 上传(这个也能通过PHP的FTP扩展完成)、HTTP 基于表单的上传、代理、cookies和用户名+密码的认证。

<?php 
$discuz_url = 'https://3water.com/';//论坛地址 
$login_url = $discuz_url .'login.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文件 
unlink($cookie_file); 
?>

以上所述就是本文的全部内容了,希望大家能够喜欢。

PHP 相关文章推荐
用PHP制作静态网站的模板框架(三)
Oct 09 PHP
实现 win2003 下 mysql 数据库每天自动备份
Dec 06 PHP
用PHP将网址字符串转换成超链接(网址或email)
May 25 PHP
ajax 的post方法实例(带循环)
Jul 04 PHP
php统计时间和内存使用情况示例分享
Mar 13 PHP
如何让thinkphp在模型中自动完成session赋值小教程
Sep 05 PHP
php异步多线程swoole用法实例
Nov 14 PHP
golang与php实现计算两个经纬度之间距离的方法
Jul 22 PHP
PHP+AjaxForm异步带进度条上传文件实例代码
Aug 14 PHP
php通过header发送自定义数据方法
Jan 18 PHP
详解laravel安装使用Passport(Api认证)
Jul 27 PHP
laravel配置Redis多个库的实现方法
Apr 10 PHP
PHP中的魔术方法总结和使用实例
May 11 #PHP
php基于curl扩展制作跨平台的restfule 接口
May 11 #PHP
PHP SPL标准库中的常用函数介绍
May 11 #PHP
PHP中的类型约束介绍
May 11 #PHP
PHP SPL标准库之接口(Interface)详解
May 11 #PHP
PHP SPL标准库之文件操作(SplFileInfo和SplFileObject)实例
May 11 #PHP
PHP设计模式之适配器模式代码实例
May 11 #PHP
You might like
php 无法载入mysql扩展
2010/03/12 PHP
memcache命令启动参数中文解释
2014/01/13 PHP
ThinkPHP中redirect用法分析
2014/12/05 PHP
CI框架源码解读之利用Hook.php文件完成功能扩展的方法
2016/05/18 PHP
Zend Framework常用校验器详解
2016/12/09 PHP
laravel使用Faker数据填充的实现方法
2019/04/12 PHP
javascript parseInt与Number函数的区别
2010/01/21 Javascript
JavaScript DOM 学习第五章 表单简介
2010/02/19 Javascript
表单提交前触发函数返回true表单才会提交
2014/03/11 Javascript
JavaScript里实用的原生API汇总
2015/05/14 Javascript
JS将unicode码转中文方法
2017/05/08 Javascript
浅谈react 同构之样式直出
2017/11/07 Javascript
Vue的路由动态重定向和导航守卫实例
2018/03/17 Javascript
Webpack 之 babel-loader文件预处理器详解
2018/03/23 Javascript
angular2实现统一的http请求头方法
2018/08/13 Javascript
Nodejs Express 通过log4js写日志到Logstash(ELK)
2018/08/30 NodeJs
vuejs 制作背景淡入淡出切换动画的实例
2018/09/01 Javascript
Vue中全局变量的定义和使用
2019/06/05 Javascript
微信小程序实现拖拽功能
2019/09/26 Javascript
JS正则表达式验证端口范围(0-65535)
2020/01/06 Javascript
ckeditor一键排版功能实现方法分析
2020/02/06 Javascript
如何使用Flask-Migrate拓展数据库表结构
2019/07/24 Python
Python 常用日期处理 -- calendar 与 dateutil 模块的使用
2020/09/02 Python
HTML5的Geolocation地理位置定位API使用教程
2016/05/12 HTML / CSS
Bose加拿大官方网站:美国知名音响品牌
2019/03/21 全球购物
优秀的教师个人的中文求职信
2013/09/21 职场文书
大学生饮食连锁店创业计划书
2014/01/17 职场文书
模范教师事迹材料
2014/02/10 职场文书
2014年个人委托书范本
2014/10/13 职场文书
领导干部作风建设工作总结
2014/10/23 职场文书
2015元旦家电促销活动策划方案
2014/12/09 职场文书
2014年社区工会工作总结
2014/12/18 职场文书
2015年人力资源部工作总结
2015/04/30 职场文书
感恩教育主题班会
2015/08/12 职场文书
七年级作文之英语老师
2019/10/28 职场文书
关于flex 上下文中自动 margin的问题(完整例子)
2021/05/20 HTML / CSS