欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

PHP模拟登录QQ邮箱获取QQ好友列表的方法

程序员文章站 2022-04-20 14:09:35
...
  1. /**

  2. * @file class.qqhttp.php
  3. * qq邮箱登陆获取类
  4. * @author wc
  5. * @site bbs.it-home.org
  6. */
  7. class QQHttp {

  8. var $cookie = '';
  9. function __cunstrut() {
  10. }
  11. function makeForm() {
  12. $form = array(
  13. 'url' => "http://mail.qq.com/cgi-bin/loginpage",
  14. );
  15. $data = $this->curlFunc($form);
  16. preg_match('/name="ts"\svalue="(\d+)"/',$data['html'], $tspre);
  17. $ts = $tspre[1];
  18. preg_match('/action="http:\/\/(m\d+)\.mail\.qq\.com/',$data['html'], $server);
  19. $server_no = $server[1];
  20. /* login.html 载入 */
  21. $html = file_get_contents(dirname(__FILE__).'/login.htm');
  22. $html = str_replace('{_ts_}',$ts, $html);
  23. $html = str_replace('{_server_no_}',$server_no, $html);
  24. return $html;
  25. }
  26. function curlFunc($array)
  27. {
  28. $ch = curl_init();
  29. curl_setopt($ch, CURLOPT_URL, $array['url']);
  30. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  31. if( isset($array['header']) && $array['header'] ) {
  32. curl_setopt($ch, CURLOPT_HEADER, 1);
  33. }
  34. if(isset($array['httpheader'])) {
  35. curl_setopt($ch, CURLOPT_HTTPHEADER, $array['httpheader']);
  36. }
  37. if(isset($array['referer'])) {
  38. curl_setopt($ch, CURLOPT_REFERER, $array['referer']);
  39. }
  40. if( isset($array['post']) ) {
  41. curl_setopt($ch, CURLOPT_POST, 1 );
  42. curl_setopt($ch, CURLOPT_POSTFIELDS, $array['post']);
  43. }
  44. if( isset($array['cookie']) ){
  45. curl_setopt($ch, CURLOPT_COOKIE, $array['cookie']);
  46. }
  47. $r['erro'] = curl_error($ch);
  48. $r['errno'] = curl_errno($ch);
  49. $r['html'] = curl_exec($ch);
  50. $r['http_code'] = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  51. curl_close($ch);
  52. return $r;
  53. }
  54. /**
  55. * 获取验证码图片和cookie
  56. * @param Null
  57. *
  58. * @return array('img'=>String, 'cookie'=>String)
  59. */
  60. function getVFCode ()
  61. {
  62. $vfcode = array(
  63. 'header' => true,
  64. 'cookie' => false,
  65. 'url'=>'http://ptlogin2.qq.com/getimage?aid='.$_GET['aid'].'&'.@$_GET['t'],
  66. );
  67. $r = $this->curlFunc($vfcode);
  68. if ($r['http_code'] != 200 ) return false;
  69. $data = split("\n", $r['html']);
  70. preg_match('/verifysession=([^;]+);/',$data[5], $temp);
  71. $cookie = trim($temp[1]);
  72. $img = $data[9];
  73. return array('img'=>$img,'cookie'=>$cookie);
  74. }
  75. /**
  76. * 登陆qq邮箱
  77. *
  78. * @param $cookie getvfcode中生成的cookie
  79. *
  80. * @return array(
  81. * sid=>String , //用户认证的唯一标示
  82. * login => Boolean, //true 登陆成功 ,false 登陆失败
  83. * server_no => String // 服务器编号
  84. * active => Boolean //true 已开通 ,false 未开通 邮箱
  85. * cookie => String // 获取数据cookie
  86. *
  87. * );
  88. */
  89. function login($cookie)
  90. {
  91. /* 生成参数字符串 */
  92. $post = array();
  93. foreach($_POST as $k => $v) {
  94. $post[] = $k.'='.urlencode($v);
  95. }
  96. $poststr = implode('&',$post);
  97. $r['server_no'] = $_GET['server_no'];
  98. $login = array(
  99. 'url'=>'http://'.$r['server_no'].'.mail.qq.com/cgi-bin/login?sid=0,2,zh_CN',
  100. 'header' => true,
  101. 'cookie' => 'verifysession='.$cookie,
  102. 'referer' => 'http://mail.qq.com/cgi-bin/loginpage',
  103. 'httpheader'=>array(
  104. "Host: " . $r['server_no'] . '.mail.qq.com',
  105. "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.0.9) Gecko/2009040821 Firefox/3.0.9 FirePHP/0.2.4",
  106. "Content-Type: application/x-www-form-urlencoded",
  107. ),
  108. 'post' => $poststr ,
  109. );
  110. $data = $this->curlFunc($login);
  111. $data['html'] = iconv("gb2312", "UTF-8", $data['html']);
  112. if ($data['http_code'] != 200) {
  113. $this->error($data);
  114. return false;
  115. }
  116. /* 测试数据 */
  117. //$data['html'] =file_get_contents('./r.txt');
  118. $r['uin'] = $_POST['uin'];
  119. /* 登陆错误的判断 */
  120. if (preg_match('|errtype=(\d)|', $data['html'], $temp_err)) {
  121. $r['login'] = false;
  122. if ($temp_err[1] == 1) {
  123. $r['msg'] = '账号和密码错误';
  124. } elseif ($temp_err[1] == 2) {
  125. $r['msg'] = '验证码错误';
  126. }
  127. return $r;
  128. }
  129. /* 登陆成功 */
  130. preg_match('|urlHead="([^"]+)"|i',$data['html'],$temp_url);
  131. $urlhead = $temp_url[1];
  132. if (preg_match('|frame_html\?sid=([^"]+)"|i',$data['html'],$temp_sid) ) {
  133. $r['sid'] = $temp_sid[1];
  134. $r['active'] = true;
  135. } elseif (preg_match('|autoactivation\?sid=([^&]+)?&|i',$data['html'],$temp_sid) ) {
  136. $r['sid'] = $temp_sid[1];
  137. $r['active'] = false;
  138. }
  139. /* 登录后cookie的获取 ,在后续操作中用到 */
  140. if (preg_match_all('|Set-Cookie:([^=]+=[^;]+)|i', $data['html'], $new_cookies) ) {
  141. $cookiestr = implode('; ', $new_cookies[1]);
  142. $cookiestr .= '; verifysession='.$cookie;
  143. }
  144. $r['login'] = true;
  145. $r['cookie'] = $cookiestr;
  146. return $r;
  147. } // bbs.it-home.org
  148. function openEmail($param)
  149. {
  150. $openEmail = array(
  151. 'url'=>'http://'.$param['server_no'].'.mail.qq.com/cgi-bin/autoactivation?actmode=6&sid='.$param['sid'],
  152. 'header' => true,
  153. 'cookie' => $param['cookie'],
  154. 'referer' => 'http://'.$param['server_no'].'mail.qq.com/cgi-bin/autoactivation?sid='.$param['sid'].'&action=reg_activate&actmode=6',
  155. 'httpheader'=>array(
  156. "Host: " . $param['server_no'] . '.mail.qq.com',
  157. 'Accept-Charset: gb2312,utf-8;q=0.7,*;q=0.7',
  158. "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.0.9) Gecko/2009040821 Firefox/3.0.9 FirePHP/0.2.4",
  159. ),
  160. );
  161. $data = $this->curlFunc($openEmail);
  162. if (preg_match('|Set-Cookie:qqmail_activated=0|i', $data['html'])) {
  163. $param['active'] = true;
  164. $param['cookie'] = $param['cookie'] .'; qqmail_activated=0; qqmail_alias=';
  165. }
  166. return $param;
  167. }
  168. /**
  169. *
  170. * 获取friends数据
  171. *
  172. * @param $param = array(
  173. * sid=>String , //用户认证的唯一标示
  174. * login => Boolean, //true 登陆成功 ,false 登陆失败
  175. * server_no => String // 服务器编号
  176. * active => Boolean //true 已开通 ,false 未开通 邮箱
  177. * cookie => String // 获取数据cookie
  178. *
  179. * );
  180. * @return Array(
  181. * key=>value, // key:qq号,value: nickname
  182. * );
  183. */
  184. function getFriends($param)
  185. {
  186. $friend = array(
  187. 'url'=>'http://'.$param['server_no'].'.mail.qq.com/cgi-bin/addr_listall?type=user&&category=all&sid='.$param['sid'],
  188. 'header' => true,
  189. 'cookie' => $param['cookie'],
  190. 'referer' => 'http://m151.mail.qq.com/cgi-bin/addr_listall?sid='.$param['sid'].'&sorttype=null&category=common',
  191. 'httpheader'=>array(
  192. "Host: " . $param['server_no'] . '.mail.qq.com',
  193. 'Accept-Charset:utf-8;q=0.7,*;q=0.7',
  194. "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.0.9) Gecko/2009040821 Firefox/3.0.9 FirePHP/0.2.4",
  195. ),
  196. );
  197. $r = $this->curlFunc($friend);
  198. if ($r['http_code'] != 200) {
  199. $this->error($r);
  200. return false;
  201. }
  202. $data = $r['html'];
  203. $preg = preg_match_all('|

    PHP模拟登录QQ邮箱获取QQ好友列表的方法]+/> ([^

    |i', $data, $temp_list);
  204. if ($preg == 0) return array();
  205. $list = array_combine($temp_list[1],$temp_list[2]);
  206. return $list;
  207. }
  208. /**
  209. * 错误显示
  210. *
  211. * @param $str array
  212. *
  213. * @return
  214. */
  215. function error($str) {
  216. $str['html'] = str_replace('script','', $str['html']);
  217. var_dump($str);
  218. exit;
  219. }
  220. }
  221. ?>
复制代码

>>> 更多 php模拟登录 文章,专题链接:php模拟登录 php curl模拟登录教程大全