本文就全是代码了。。。
$cookiefile = tempnam( './log/' , 'cookie' );//设定cookie文件的路径 。 $ch = curl_init(); $header[]="Content-Type: application/x-www-form-urlencoded"; curl_setopt($ch, CURLOPT_URL, 'http://www.linezing.com/login.php'); //登录地址 curl_setopt($ch, CURLOPT_HTTPHEADER, $header); //发送header ,其实这个header可以不发送 curl_setopt($ch, CURLOPT_POST, 1); //这是POST数据 curl_setopt($ch, CURLOPT_POSTFIELDS, 'referer=&webname=index&username=用户名&password=密码&submit=%E7%99%BB%E5%BD%95');//http_build_query( $postData)); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //这个是代表curl_exec后取返回成字符串,而不是象WEB一样跳转 curl_setopt($ch, CURLOPT_HEADER, 0); //curl返回的时候,默认都是带有header信息的,所以这里设为0,代表返回的时候不要header信息 curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate'); //这是在用sniff抓包的时候发现用了gzip,deflate的encoding, curl_setopt($ch, CURLOPT_REFERER, 'http://www.linezing.com/');//记录来源的Referer curl_setopt($ch, CURLOPT_COOKIEFILE,$cookiefile); curl_setopt($ch, CURLOPT_COOKIEJAR,$cookiefile); curl_exec($ch); //我这里并没有取返回值,主要是把cookie记录下来 curl_setopt($ch, CURLOPT_URL, 'http://www.linezing.com/router.php'); //登录后跳转的网址 //curl_setopt($ch, CURLOPT_COOKIEFILE,$cookiefile); 原先我在这里也记录cookie了,但事实上,我这样做之后,反而会把第一次登录时的cookie覆盖了。。。郁闷 //curl_setopt($ch, CURLOPT_COOKIEJAR,$cookiefile); curl_setopt($ch, CURLOPT_REFERER, 'http://www.linezing.com/'); curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_COOKIESESSION,1); $res = curl_exec($ch); curl_setopt($ch, CURLOPT_URL, 'http://tongji.linezing.com/welcome.html'); //welcome页还会再判断是否登录,如果没有登录,会是一段JS跳到www.linezing.com //curl_setopt($ch, CURLOPT_COOKIEFILE,$cookiefile); //curl_setopt($ch, CURLOPT_COOKIEJAR,$cookiefile); curl_setopt($ch, CURLOPT_REFERER, 'http://www.linezing.com/'); curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_COOKIESESSION,1); curl_exec($ch); curl_setopt($ch, CURLOPT_URL, 'http://tongji.linezing.com/mystat.html'); curl_setopt($ch, CURLOPT_COOKIEFILE,$cookiefile); curl_setopt($ch, CURLOPT_COOKIEJAR,$cookiefile); curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_COOKIESESSION,1); $res = curl_exec($ch); //这里获取返回值,我是想看一下是不是正确。。。 curl_close($ch); echo htmlSpecialChars( $res );
因为是临时写的代码,所以没有封装,但也算是尽量做了注释。。
我来顶一下膘叔