SNOOPY 实现模拟登陆
采集论坛第一步就是要模拟登陆,由于各个站点登录表单各不相同,验证方式又是多种多样,所以直接提交用户名密码 到登录页面就比较繁琐。
所以我们采用cookie来模拟登陆无疑是最佳捷径。
对象www.discuz.net
用户名liuyuntest
密码123456
一 首先我们手工登录下,记录下cookie
dznet_cookietime=2592000;dznet_onlineusernum=7816;dznet_sid=QtlC87;
dznet_auth=6bbeCQrzGv4eliNMLgU%2FlGZSpzbrsauGO1l0OBp6VQw5p0bcEg0xd4slYCM2ks%2FL0YCVYSO7XP2z8GMaxkPDUbXZCWft;
checkpm=1
(目前主流浏览都有插件查看cookie)
二 采用snoopy模拟登录
- include("snoopy.php");
- $snoopy = new Snoopy;
- $snoopy->agent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; TheWorld)";//这项是浏览器信息,前面你用什么浏览器查看cookie,就用那个浏览器的信息(ps:$_SERVER可以查看到浏览器的信息)
- $snoopy->referer = "http://www.discuz.net/";
- $snoopy->rawheaders["COOKIE"]= "dznet_cookietime=2592000;dznet_onlineusernum=7816;dznet_sid=QtlC87;
- dznet_auth=6bbeCQrzGv4eliNMLgU%2FlGZSpzbrsauGO1l0OBp6VQw5p0bcEg0xd4slYCM2ks%2FL0YCVYSO7XP2z8GMaxkPDUbXZCWft;
- checkpm=1";
- $snoopy->fetch("http://www.discuz.net/post.php?action=newthread&fid=2&extra=page%3D1");//获取发贴页面
- echo $tempCn = $snoopy->results;
三 采用curl 模拟登录
- $url = "http://www.discuz.net/";
- $useragent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; TheWorld)";
- $cookie = "dznet_cookietime=2592000;dznet_onlineusernum=7816;dznet_sid=QtlC87;
- dznet_auth=6bbeCQrzGv4eliNMLgU%2FlGZSpzbrsauGO1l0OBp6VQw5p0bcEg0xd4slYCM2ks%2FL0YCVYSO7XP2z8GMaxkPDUbXZCWft;
- checkpm=1";
- $ch= curl_init();
- curl_setopt($ch, CURLOPT_URL,$url);
- curl_setopt($ch, CURLOPT_COOKIE, $cookie);
- curl_setopt($ch, CURLOPT_REFERER, "http://www.discuz.net/index.php");
- curl_setopt($ch, CURLOPT_HEADER, false);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt ($ch, CURLOPT_USERAGENT, $useragent);