curl模拟登录

 1 <?php
 2 $post_data = array("username"=>"yuejide@163.com","password"=>"yuejide198225","remember"=>0); 
 3 $data = http_build_query($post_data); 
 4 $ch = curl_init(); 
 5 curl_setopt($ch, CURLOPT_URL, "http://www.imooc.com/user/login"); 
 6 curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);//不要直接输出 
 7 
 8 date_default_timezone_set('PRC'); 
 9 curl_setopt($ch, CURLOPT_COOKIESESSION,1); 
10 curl_setopt($ch, CURLOPT_COOKIEFILE,"cookiefile"); 
11 curl_setopt($ch, CURLOPT_COOKIEJAR,"cookiefile"); 
12 curl_setopt($ch, CURLOPT_COOKIE,session_name().'='.session_id()); 
13 curl_setopt($ch, CURLOPT_HEADER,0); 
14 //这样设置能够让curl支持页面跳转 
15 curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1); 
16 curl_setopt($ch, CURLOPT_POST,1); 
17 curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 
18 curl_setopt($ch, CURLOPT_HTTPHEADER,array("application/x-www-form-urlencoded;charset=utf8","Content-length:".strlen($data))); 
19 curl_exec($ch);//执行 
20 curl_setopt($ch, CURLOPT_URL, "http://www.imooc.com/space/index"); 
21 curl_setopt($ch, CURLOPT_POST,0); 
22 curl_setopt($ch, CURLOPT_HTTPHEADER,array("Content-type:text/html")); 
23 $output = curl_exec($ch); 
24 curl_close($ch); 
25 //打印数据 
26 echo $output;

 

posted @ 2015-04-19 21:10  北京流浪儿  阅读(207)  评论(0编辑  收藏  举报