curl模拟登录

$post_data = array("username"=>"yuejide@163.com","password"=>"yuejide198225","remember"=>0); 
$data = http_build_query($post_data); 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, "http://www.imooc.com/user/login"); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);//不要直接输出 

date_default_timezone_set('PRC'); 
curl_setopt($ch, CURLOPT_COOKIESESSION,1); 
curl_setopt($ch, CURLOPT_COOKIEFILE,"cookiefile"); 
curl_setopt($ch, CURLOPT_COOKIEJAR,"cookiefile"); 
curl_setopt($ch, CURLOPT_COOKIE,session_name().'='.session_id()); 
curl_setopt($ch, CURLOPT_HEADER,0); 
//这样设置能够让curl支持页面跳转 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1); 
curl_setopt($ch, CURLOPT_POST,1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 
curl_setopt($ch, CURLOPT_HTTPHEADER,array("application/x-www-form-urlencoded;charset=utf8","Content-length:".strlen($data))); 
curl_exec($ch);//执行 
curl_setopt($ch, CURLOPT_URL, "http://www.imooc.com/space/index"); 
curl_setopt($ch, CURLOPT_POST,0); 
curl_setopt($ch, CURLOPT_HTTPHEADER,array("Content-type:text/html")); 
$output = curl_exec($ch); 
curl_close($ch); 
//打印数据 
echo $output;

 

posted @ 2015-08-02 12:37  北京流浪儿  阅读(298)  评论(0编辑  收藏  举报