您的位置:首页 > 其它

curl模拟登陆获取cookie

2014-04-22 15:36 351 查看
$ch = curl_init();
$url = 'http://portal.jnu.edu.cn/portal';
//这里要设定cookie保存的位置,注意要dirname(__FILE__) 
$cookie_jar = dirname(__FILE__)."/pic.cookie";
//$url = 	'http://portal.jnu.edu.cn/portal/media-type/html/role/user/page/edit-account';
$post = array('username'=>'***','password'=>'****','userNameType'=>'cardID','submit'=>'登陆','action'=>'JnuLogin');

curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_HEADER, 0);
// 对认证证书来源的检查,0表示阻止对证书的合法性的检查。
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

// 从证书中检查SSL加密算法是否存在
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 1);

//模拟用户使用的浏览器,在HTTP请求中包含一个”user-agent”头的字符串。
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");
//这里防止302跳转
curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);

$referer = 'http://portal.jnu.edu.cn/portal/role/guest/page/login';
curl_setopt($ch, CURLOPT_REFERER, $referer); //构造来路

// 发送一个常规的POST请求,类型为:application/x-www-form-urlencoded,就像表单提交的一样。
curl_setopt($ch, CURLOPT_POST, 1);
//这里要使用http_build_query整合post数据
 curl_setopt($ch, CURLOPT_POSTFIELDS,  http_build_query($post));
curl_setopt($ch,CURLOPT_AUTOREFERER,true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_jar);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_jar);

//$result = curl_exec($ch);
if (curl_errno($ch)) {
print curl_error($ch);
}

curl_close($ch);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: