+ -
当前位置:首页 → 问答吧 → PHP获取163、sina、sohu、yahoo、126、gmail、tom邮箱联系人地址【更新于 2009.10.21】

PHP获取163、sina、sohu、yahoo、126、gmail、tom邮箱联系人地址【更新于 2009.10.21】

时间:2009-12-28

来源:互联网

声明:PPC上本文作者jvones=miikey(哈哈)!
最近一直忙于项目上的QQ、MSN、邮箱(目前以实现163、126、gmail、sina、tom、sohu、yahoo等)通讯录地址获取;其中搜狐失效还需研究一下!
     在网上找了一些,大部分都已经失效,为此我重新整理了一下;特别放出126的代码,163是比较容易抓取的;126有点变态多了一次跳转,比较麻烦;
     QQ邮箱联系人导出 由于腾讯查的厉害,就不奉上了!
     另外由同行朋友:iflower  对sohu,gmail,yahoo部分地方进行修改,程序基本完善,有需要研究的同志,请联系我!大家一起完善并跟进邮箱抓去程序;
在线demo地址:http://www.miikey.com/blog/?p=200
.[    post]
复制代码
  1. <?php
  2. /**
  3. * @file class.126http.php
  4. * 获得126邮箱通讯录列表
  5. * @author jvones<[email protected]> http://www.miikey.com/blog
  6. * @date 2009-09-26
  7. **/
  8. class http126
  9. {
  10.         private function login($username, $password)
  11.         {                
  12.                 //第一步:初步登陆
  13.                 $cookies = array();
  14.                 $ch = curl_init();
  15.                 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  16.                 
  17.                 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");
  18.                 curl_setopt($ch, CURLOPT_POST, 1);
  19.                 curl_setopt($ch, CURLOPT_POSTFIELDS, "username=".$username."@126.com&password=".$password);
  20.                 
  21.                 curl_setopt($ch, CURLOPT_COOKIEJAR, COOKIEJAR);
  22.                 curl_setopt($ch,CURLOPT_HEADER,1);                
  23.                 curl_setopt($ch, CURLOPT_TIMEOUT, TIMEOUT);
  24.                 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  25.                 $str = curl_exec($ch);        
  26.                 //file_put_contents('./126result.txt', $str);                
  27.                 curl_close($ch);
  28.                         
  29.                 //获取redirect_url跳转地址,可以从126result.txt中查看,通过正则在$str返回流中匹配该地址
  30.                 preg_match("/replace\(\"(.*?)\"\)\;/", $str, $mtitle);
  31.                 $_url1 = $mtitle[1];
  32.                 
  33.                 //file_put_contents('./126resulturl.txt', $redirect_url);        
  34.                 //第二步:再次跳转到到上面$_url1
  35.                 $ch = curl_init($_url1);                
  36.                 
  37.                 curl_setopt($ch, CURLOPT_TIMEOUT, TIMEOUT);
  38.                 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  39.                 curl_setopt($ch,CURLOPT_COOKIEFILE,COOKIEJAR);
  40.                 curl_setopt($ch, CURLOPT_COOKIEJAR, COOKIEJAR);                
  41.                 curl_setopt($ch,CURLOPT_HEADER,1);        
  42.                 $str2 = curl_exec($ch);
  43.                 curl_close($ch);
  44.                                                 
  45.                 if (strpos($contents, "安全退出") !== false)
  46.                 {                        
  47.                         return 0;
  48.                 }                
  49.                 return 1;
  50.         }
  51.         
  52.         /**
  53.          * 获取邮箱通讯录-地址
  54.          * @param $user
  55.          * @param $password
  56.          * @param $result
  57.          * @return array
  58.          */
  59.         public function getAddressList($username, $password)
  60.         {                
  61.                 if (!$this->login($username, $password))
  62.                 {
  63.                         return 0;
  64.                 }
  65.             
  66.                 $header = $this->_getheader($username);
  67.                 if (!$header['sid'])
  68.         {
  69.             return 0;
  70.         }
  71.         
  72.         //测试找出sid(很重要)和host
  73.         //file_put_contents('./host.txt', $header['host']);
  74.         //file_put_contents('./sid.txt', $header['sid']);
  75.         
  76.                 //开始进入模拟抓取
  77.                 $ch = curl_init();
  78.                 curl_setopt($ch, CURLOPT_URL, "http://".$header['host']."/a/s?sid=".$header['sid']."&func=global:sequential");
  79.                 curl_setopt($ch, CURLOPT_COOKIEFILE, COOKIEJAR);
  80.                 curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/xml"));
  81.                 $str = "<?xml version=\"1.0\"?><object><array name=\"items\"><object><string name=\"func\">pab:searchContacts</string><object name=\"var\"><array name=\"order\"><object><string name=\"field\">FN</string><boolean name=\"ignoreCase\">true</boolean></object></array></object></object><object><string name=\"func\">user:getSignatures</string></object><object><string name=\"func\">pab:getAllGroups</string></object></array></object>";
  82.                 curl_setopt($ch, CURLOPT_POST, 1);
  83.                 curl_setopt($ch, CURLOPT_POSTFIELDS, $str);
  84.                 curl_setopt($ch, CURLOPT_TIMEOUT, TIMEOUT);
  85.                 ob_start();
  86.                 curl_exec($ch);
  87.                 $contents = ob_get_contents();
  88.                 ob_end_clean();
  89.                 curl_close($ch);
  90.                 
  91.         //get mail list from the page information username && emailaddress
  92.         preg_match_all("/<string\s*name=\"EMAIL;PREF\">(.*)<\/string>/Umsi",$contents,$mails);
  93.         preg_match_all("/<string\s*name=\"FN\">(.*)<\/string>/Umsi",$contents,$names);
  94.         $users = array();
  95.         foreach($names[1] as $k=>$user)
  96.         {
  97.             //$user = iconv($user,'utf-8','gb2312');
  98.             $users[$mails[1][$k]] = $user;
  99.         }
  100.         if (!$users)
  101.         {
  102.             return '您的邮箱中尚未有联系人';
  103.         }      
  104.         
  105.         return $users;
  106.         }
  107.         
  108.         /**
  109.     * Get Header info
  110.     */
  111.     private function _getheader($username)
  112.     {
  113.                 $ch = curl_init();
  114.                 curl_setopt($ch, CURLOPT_URL, "http://entry.mail.126.com/cgi/ntesdoor?hid=10010102&lightweight=1&verifycookie=1&language=0&style=-1&username=".$username."@126.com");
  115.                 curl_setopt($ch, CURLOPT_COOKIEFILE, COOKIEJAR);  //当前使用的cookie
  116.                 curl_setopt($ch, CURLOPT_COOKIEJAR, COOKIEJAR);   //服务器返回的新cookie
  117.                 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  118.                 curl_setopt($ch, CURLOPT_HEADER, true);
  119.                 curl_setopt($ch, CURLOPT_NOBODY, true);
  120.                 $content=curl_exec($ch);
  121.                 
  122.                 preg_match_all('/Location:\s*(.*?)\r\n/i',$content,$regs);
  123.         $refer = $regs[1][0];
  124.         preg_match_all('/http\:\/\/(.*?)\//i',$refer,$regs);                
  125.         $host = $regs[1][0];
  126.         preg_match_all("/sid=(.*)/i",$refer,$regs);
  127.         $sid = $regs[1][0];
  128.                 
  129.                 curl_close($ch);
  130.                 return array('sid'=>$sid,'refer'=>$refer,'host'=>$host);
  131.     }
  132. }
  133. ?>


[/post]
[ 此帖被miikey在2009-12-28 11:55重新编辑 ]

作者: miikey   发布时间: 2009-12-28

作者: aluo   发布时间: 2009-12-28

强大学习

作者: hbtsdb   发布时间: 2009-12-28

作者: miikey   发布时间: 2010-01-04