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

不久前抽出的功课是从二个网站获得商品库存,可是这么些网址供给登入,作者用fsockopen传递了整个header头都不算,只好求助于curl了。
顺手说一下curl模块的打开办法:
(1卡塔尔国从php目录下拷贝:libeay32.dll,ssleay32.dll 到windows目录下。
(2)打开php.ini,查找“extension_dir =
xxxxx”,确认前边的文件目录内有php_curl.dll文件。
(3)同样是php.ini,查找“extension=php_curl.dll”,确认它未有被讲解(前边未有’;’卡塔尔。
(4State of Qatar重启apache,假设利用curl_init(卡塔尔国;语句现身谬误提示,则表达未有安装成功。

<?php
$discuz_url = ‘
$login_url = $discuz_url .’logging.php?action=login’;//登陆页地址

php的curl真的是风姿罗曼蒂克对黄金年代好用,网络大器晚成搜索相关小说都以有关curl模拟登录的,少之又少人提供模拟discuz发贴的源码。

复制代码 代码如下:
$curl = curl_init();
$cookie_jar = tempnam(‘./tmp’,’cookie’);
curl_setopt($curl,
CURLOPT_URL,’
curl_setopt($curl, CURLOPT_POST, 1);
$request = ‘user=xxx&password=xxx’;
curl_setopt($curl, CURLOPT_POSTFIELDS, $request);//传 递数据
curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie_jar卡塔尔国;//
把再次回到来的cookie新闻保存在$cookie_jar文件中
curl_setopt($curl, CURLOPT_RETU途达NTRANSFE大切诺基, 1State of Qatar;//设定再次回到的数量是还是不是自动展现
curl_setopt($curl, CURLOPT_HEADEGL450, false卡塔尔(قطر‎;//设定是不是出示头信 息
curl_setopt($curl, CURLOPT_NOBODY, false卡塔尔国;//设定是或不是输出页面 内容
curl_exec($curl卡塔尔国;//重回结果
curl_close($curl); //关闭

$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’] = ”;

本着分享的标准,作者把团结测量检验成功的发帖代码贴出来。不足的地方,希望大家指正。

$curl2 = curl_init();
curl_setopt($curl2, CURLOPT_URL,

curl_setopt($curl2, CURLOPT_HEADER, false);
curl_setopt($curl2, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl2, CURLOPT_COOKIEFILE, $cookie_jar);
$content = curl_exec($curl2);

//获取表单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.’);
}

<?php
$discuz_url = ‘
$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’] = ”;

//POST数据,获取老董KIE,cookie文件放在网址的temp目录下
$cookie_file = tempnam(‘./temp’,’cookie’);

//获取表单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.’);
}

$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);

//POST数据,获取首席营业官KIE,cookie文件放在网址的temp目录下
$cookie_file = tempnam(‘./temp’,’cookie’);

//取到了主要的cookie文件就能够带着cookie文件去模拟发帖,fid为论坛的栏目ID
$send_url = $discuz_url.”post.php?action=newthread&fid=2″;

$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);

$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);

//取到了器重的cookie文件就足以带着cookie文件去模拟发帖,fid为论坛的栏目ID
$send_url = $discuz_url.”post.php?action=newthread&fid=2″;

//这里的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.’);
}

$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);

$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;

//这里的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.’);
}

$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);

$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;

//清理cookie文件
unlink($cookie_file);

$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);

?>

 


发表评论

电子邮件地址不会被公开。 必填项已用*标注