当前位置:Gxlcms > PHP教程 > PHP获取QQ邮箱好友的具体代码讲解_PHP教程

PHP获取QQ邮箱好友的具体代码讲解_PHP教程

时间:2021-07-01 10:21:17 帮助过:5人阅读

目前,大家都在使用QQ,并且还将QQ附带的一些其他软件当做自己的日常使用工具。今天我们就向大家介绍有关如何运用具体的PHP获取QQ邮箱好友的代码如下:

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

怎么样,通过以上对于PHP获取QQ邮箱好友的代码的介绍,大家是否已经完全掌握了呢?


www.bkjia.comtruehttp://www.bkjia.com/PHPjc/446439.htmlTechArticle目前,大家都在使用QQ,并且还将QQ附带的一些其他软件当做自己的日常使用工具。今天我们就向大家介绍有关如何运用 具体的PHP获取QQ邮箱...

人气教程排行