缘来路过 发表于 2015-8-29 09:13:02

php curl模拟登陆163邮箱

  李恺的博客已迁移到youyuge.com
  原文地址: http://www.lokizone.com/blog/read.php?73
  首先装个抓包的工具
ie下装httpwatch
ff下装httpfox
本文用的是httpfox
直接看代码吧

代码

1 <?php
2error_reporting(0);
3
4//邮箱用户名(不带@163.com后缀的)
5$user='****';
6//邮箱密码
7$pass='****';
8//目标邮箱
9 //$mail_addr= 'loki_wuxi@163.com';
10
11 //登陆
12 $url='http://reg.163.com/logins.jsp?type=1&url=http://entry.mail.163.com/coremail/fcg/ntesdoor2?lightweight%3D1%26verifycookie%3D1%26language%3D-1%26style%3D-1';
13 $ch= curl_init($url);
14 //创建一个用于存放cookie信息的临时文件
15 $cookie=tempnam('.','~');
16 $referer_login='http://mail.163.com';
17 //返回结果存放在变量中,而不是默认的直接输出
18 curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
19 curl_setopt($ch, CURLOPT_HEADER,true);
20 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT,120);
21 curl_setopt($ch, CURLOPT_POST,true);
22
23 curl_setopt($ch, CURLOPT_REFERER,$referer_login);
24
25 $fields_post=array(
26 'username'=>$user,
27 'password'=>$pass,
28 'verifycookie'=>1,
29 'style'=>-1,
30 'product'=>'mail163',
31 'selType'=>-1,
32 'secure'=>'on'
33 );
34
35 $headers_login=array(
36 'User-Agent'=>'Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9) Gecko/2008052906 Firefox/3.0',
37 'Referer'=>'http://www.163.com'
38 );
39
40 $fields_string='';
41
42 foreach($fields_postas$key=>$value)
43 {
44 $fields_string.=$key.'='.$value.'&';
45 }
46
47 $fields_string=rtrim($fields_string,'&');
48
49 curl_setopt($ch, CURLOPT_COOKIESESSION,true);
50 //关闭连接时,将服务器端返回的cookie保存在以下文件中
51 curl_setopt($ch, CURLOPT_COOKIEJAR,$cookie);
52 curl_setopt($ch, CURLOPT_HTTPHEADER,$headers_login);
53 curl_setopt($ch, CURLOPT_POST,count($fields));
54 curl_setopt($ch, CURLOPT_POSTFIELDS,$fields_string);
55
56 $result= curl_exec($ch);
57 curl_close($ch);
58
59
60 //跳转
61 $url='http://entry.mail.163.com/coremail/fcg/ntesdoor2?lightweight=1&verifycookie=1&language=-1&style=-1&username=loki_wuxi';
62
63 $ch= curl_init($url);
64
65 $headers=array(
66 'User-Agent'=>'Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9) Gecko/2008052906 Firefox/3.0'
67 );
68
69 curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
70 curl_setopt($ch, CURLOPT_HEADER,true);
71 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT,120);
72 curl_setopt($ch, CURLOPT_POST,true);
73 curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);
74 //将之前保存的cookie信息,一起发送到服务器端
75 curl_setopt($ch, CURLOPT_COOKIEFILE,$cookie);
76 curl_setopt($ch, CURLOPT_COOKIEJAR,$cookie);
77 $result= curl_exec($ch);
78 curl_close($ch);
79
80 //取得sid
81 preg_match('/sid=[^\"].*/',$result,$location);
82 $sid=substr($location,4,-1);
83 //file_put_contents('./result.txt', $sid);
84
85
86 //通讯录地址
87 $url='http://g4a30.mail.163.com/jy3/address/addrlist.jsp?sid='.$sid.'&gid=all';
88 $ch= curl_init($url);
89
90 $headers=array(
91 'User-Agent'=>'Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9) Gecko/2008052906 Firefox/3.0'
92 );
93
94 curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
95 curl_setopt($ch, CURLOPT_HEADER,true);
96 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT,120);
97 curl_setopt($ch, CURLOPT_POST,true);
98 curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);
99 curl_setopt($ch, CURLOPT_COOKIEFILE,$cookie);
100 curl_setopt($ch, CURLOPT_COOKIEJAR,$cookie);
101 $result= curl_exec($ch);
102 curl_close($ch);
103 //file_put_contents('./result.txt', $result);
104 unlink($cookie);
105
106 //开始抓取内容
107 preg_match_all('/<td class="Ibx_Td_addrName"><a[^>]*>(.*?)<\/a><\/td><td class="Ibx_Td_addrEmail"><a[^>]*>(.*?)<\/a><\/td>/i',$result,$infos,PREG_SET_ORDER);
108 //1:姓名2:邮箱
109 print_r($infos);
110
111 /*
112* 下面就可以为所欲为了- -
113 */
114 ?>
115   
  上面
  

foreach($fields_post as $key => $value)
{
    $fields_string .= $key . '=' . $value . '&';
}  foreach($fields_post as $key => $value)
  {   
  $fields_string .= $key . '=' . $value . '&';
  }
  可以写成
  $fields_string = http_build_query($fields_post);
  
页: [1]
查看完整版本: php curl模拟登陆163邮箱