PageRenderTime 50ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/getmail/126.php

https://github.com/jerywang/phplib
PHP | 130 lines | 90 code | 8 blank | 32 comment | 3 complexity | b3a9a962ae30dc441bba27c4914baf98 MD5 | raw file
  1. <?php
  2. /**
  3. * @name Http126
  4. * 导出126邮箱联系人
  5. * @author Spring
  6. */
  7. header("Content-type: text/html; charset=utf-8");
  8. define("COOKIEJAR1", tempnam("./assets/tmp", "c1_"));
  9. define("COOKIEJAR2", tempnam("./assets/tmp", "c2_"));
  10. define('TIMEOUT', 60);
  11. class Http126{
  12. /**
  13. * @desc: login in the 126 mail box
  14. * @param string $username
  15. * @param string $password
  16. * @return int //the login status
  17. */
  18. public $agent="Mozilla/5.0 (Windows NT 6.1) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.57 Safari/536.11";
  19. public function login($username, $password){
  20. $url = 'http://reg.163.com/login.jsp?type=1&product=mail126&url=http://entry.mail.126.com/cgi/ntesdoor?hid%3D10010102%26lightweight%3D1%26language%3D0%26style%3D-1';
  21. $ch = curl_init();
  22. curl_setopt($ch, CURLOPT_FOLLOWLOCATION,true);
  23. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  24. curl_setopt($ch, CURLOPT_URL, "https://reg.163.com/logins.jsp?type=1&product=mail126&url=http://entry.mail.126.com/cgi/ntesdoor?hid%3D10010102%26lightweight%3D1%26verifycookie%3D1%26language%3D0%26style%3D-1");
  25. curl_setopt($ch, CURLOPT_POST, true);
  26. curl_setopt($ch, CURLOPT_HTTPHEADER, array('User-Agent' => $this->agent));
  27. curl_setopt($ch, CURLOPT_POSTFIELDS, "username=".$username."&password=".$password);
  28. curl_setopt($ch, CURLOPT_COOKIEJAR, COOKIEJAR1);
  29. //curl_setopt($ch, CURLOPT_HTTPHEADER, $headers_login);
  30. curl_setopt($ch,CURLOPT_HEADER,true);
  31. curl_setopt($ch, CURLOPT_TIMEOUT, TIMEOUT);
  32. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  33. $str = curl_exec($ch);
  34. //file_put_contents('./126_login.txt', $str);
  35. //file_put_contents('./126_cookie.txt', COOKIEJAR1);
  36. curl_close($ch);
  37. /*=====开始第一次跳转=====*/
  38. preg_match('/http:\/\/passport.126.com(.*)loginyoudao=0/', $str, $url_c_1);//取得跳转地址
  39. $ch = curl_init($url_c_1[0]);
  40. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  41. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  42. curl_setopt($ch, CURLOPT_HTTPHEADER, array('User-Agent' => $this->agent));
  43. curl_setopt($ch, CURLOPT_REFERER, $url);
  44. curl_setopt($ch, CURLOPT_COOKIEFILE, COOKIEJAR1);
  45. curl_setopt($ch, CURLOPT_COOKIEJAR, COOKIEJAR2);
  46. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, TIMEOUT);
  47. curl_setopt($ch, CURLOPT_TIMEOUT, TIMEOUT);
  48. curl_setopt($ch, CURLOPT_HEADER, true);
  49. $result_check_1 = curl_exec($ch);
  50. //file_put_contents('126_login2.txt', $result_check_1);
  51. curl_close($ch);
  52. if (strpos($result_check_1, "登录成功") === false){
  53. return 0;
  54. }
  55. return 1;
  56. }
  57. /**
  58. * 获取邮箱通讯录-地址
  59. * @param $username
  60. * @param $password
  61. * @return array
  62. */
  63. public function getAddressList($username, $password){
  64. if (!$this->login($username, $password)){
  65. return 0;
  66. }
  67. $header = $this->_getheader($username);
  68. if (!$header['sid']){
  69. return 0;
  70. }
  71. //开始进入模拟抓取
  72. $ch = curl_init();
  73. $url='http://twebmail.mail.126.com/jy3/address/addrlist.jsp?sid='.$header['sid'].'&gid=all';
  74. curl_setopt($ch, CURLOPT_URL, $url);
  75. curl_setopt($ch, CURLOPT_COOKIEFILE, COOKIEJAR1);
  76. curl_setopt($ch, CURLOPT_HTTPHEADER, array('User-Agent' => $this->agent));
  77. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  78. curl_setopt($ch, CURLOPT_TIMEOUT, TIMEOUT);
  79. $ret = curl_exec($ch);
  80. curl_close($ch);
  81. //file_put_contents('126_data.txt', $ret);
  82. preg_match_all('/<td\s*class="Ibx_Td_addrEmail"><a[^>]+>[^>]+a><\/td>/',$ret,$mails);
  83. preg_match_all('/<td\s*class="Ibx_Td_addrName"><a[^>]+>[^>]+a><\/td>/',$ret,$names);
  84. //print_r($mails);print_r($names);
  85. foreach ($mails[0] as $k=>$v){
  86. $r['email'] = strip_tags($v);
  87. $r['name'] = strip_tags($names[0][$k]);
  88. $res[] = $r;
  89. }
  90. return $res;
  91. }
  92. /**
  93. * Get Header info
  94. */
  95. public function _getheader($username){
  96. $url="http://entry.mail.126.com/cgi/ntesdoor?username=".$username."&hid=10010102&lightweight=1&verifycookie=1&language=0&style=-1";
  97. $ch = curl_init();
  98. curl_setopt($ch, CURLOPT_URL, $url);
  99. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  100. curl_setopt($ch, CURLOPT_COOKIEFILE, COOKIEJAR2); //当前使用的cookie
  101. curl_setopt($ch, CURLOPT_COOKIEJAR, COOKIEJAR1); //服务器返回的新cookie
  102. curl_setopt($ch, CURLOPT_HTTPHEADER, array('User-Agent' => $this->agent));
  103. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  104. curl_setopt($ch, CURLOPT_HEADER, true);
  105. curl_setopt($ch, CURLOPT_NOBODY, true);
  106. curl_setopt($ch, CURLOPT_TIMEOUT, TIMEOUT);
  107. $content=curl_exec($ch);
  108. //file_put_contents('126_header.txt', $content);
  109. preg_match_all('/Location:\s*(.*?)\r\n/i',$content,$regs);
  110. $refer = $regs[1][0];
  111. preg_match_all('/http\:\/\/(.*?)\//i',$refer,$regs);
  112. $host = $regs[1][0];
  113. preg_match_all("/sid=(.*)/i",$refer,$regs);
  114. $sid = $regs[1][0];
  115. curl_close($ch);
  116. return array('sid'=>$sid,'refer'=>$refer,'host'=>$host);
  117. }
  118. }
  119. // $e126=new Http126();
  120. // $elist = $e126->getAddressList('jerrywang4444@126.com', 'abcd1234');
  121. // print_r($elist);
  122. ?>