<?php
/**
* --------------------------------
* Curl模拟登陆操作
* --------------------------------
* @author Corwien
* @version 2017-01
* --------------------------------
*/
$url='登陆网址';
$post=array(登陆的信息);
//模拟登陆
function send_http($url, $post, $timeout = 15, $header = array(), $connecttimeout = 5) {
$cookie_file=dirname(__FILE__).'/cookies.txt';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
//curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; rv:19.0) Gecko/20100101 Firefox/19.0');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $connecttimeout);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file); //存储cookies
if ($header){
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
}
if($post){
if(is_array($post)){
$post = http_build_query($post);
}
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
}
$rs = curl_exec($ch);
$http_info = curl_getinfo($ch);
//print_r($http_info);
curl_close($ch);
if($http_info['http_code'] != 200 && $http_info['http_code'] != 302 ){
return false;
}
return $rs;
}
// 登录成功后获取数据
function get_content($url, $cookie) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//如果遇到ssl验证加上下面这段
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie); //读取cookie
$rs = curl_exec($ch); //执行cURL抓取页面内容
curl_close($ch);
return $rs;
}
// 登录成功后模拟发帖
function post_thread($url, $cookie, $post)
{
$curl = curl_init();//初始化curl模块
curl_setopt($curl, CURLOPT_URL, $url);//登录提交的地址
curl_setopt($curl, CURLOPT_HEADER, 0);//是否显示头信息
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 0);//是否自动显示返回的信息
curl_setopt($curl, CURLOPT_COOKIEFILE, $cookie); //读取cookie
curl_setopt($curl, CURLOPT_POST, 1);//post方式提交
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post));//要提交的信息
curl_exec($curl);//执行cURL
curl_close($curl);//关闭cURL资源,并且释放系统资源
}
//设置post的数据
$post = array (
'user_id' => '123456@qq.com',
'password' => '123456',
'goto_page' => 'http://m.app.cn/index.php',
'act' => 'login',
't' => time(),
);
//登录地址
$url = "http://m.app.cn/account/login.php";
//设置cookie保存路径
$cookie = dirname(__FILE__) . '/cookie_curl.txt';
//登录后要获取信息的地址
$url2 = "http://m.app.cn/user/wap/my_index.php";
// 1.模拟登录
login_post($url, $cookie, $post);
// 2.获取登录页的信息
// $content = get_content($url2, $cookie);
//匹配页面信息
// $preg = "/<td class='portrait'>(.*)<\/td>/i";
// preg_match_all($preg, $content, $arr);
// $str = $arr[1][0];
//输出内容
// echo $content;
// 3.模拟发帖
$thread_info = array(
'action' => 'pub',
'title' => 'Test curl',
'content' => 'Hello, world.',
't' => time(),
);
$pub_thread_url = 'http://m.app.cn/thread/api/pub_thread.php';
$ret = post_thread($pub_thread_url, $cookie, $thread_info);
print_r($ret);
//删除cookie文件
@ unlink($cookie);
?>
Curl模拟提交数据
最新推荐文章于 2024-03-19 17:09:40 发布