Bookmark and Share

Lee's 程序人生

HTML CSS Javascript XML AJAX ATLAS C# C++ 数据结构 软件工程 设计模式 asp.net Java 数字图象处理 Sql 数据库
  博客园  :: 首页  :: 新随笔  :: 联系 :: 管理

转:PHP获取163、gmail、126等邮箱联系人地址

Posted on 2009-10-12 18:30  analyzer  阅读(1119)  评论(0编辑  收藏  举报
PHP代码
  1. <?php   
  2. /**   
  3. * @file class.126http.php  
  4. * 获得126邮箱通讯录列表  
  5. * @author jvones<jvones@gmail.com> http://www.jvones.com/blog  
  6. * @date 2009-09-26  
  7. **/  
  8.   
  9. class http126   
  10. {   
  11.   
  12.         private function login($username$password)   
  13.         {                   
  14.                 //第一步:初步登陆   
  15.                 $cookies = array();   
  16.                 $ch = curl_init();   
  17.                 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);   
  18.                    
  19.                 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");   
  20.                 curl_setopt($ch, CURLOPT_POST, 1);   
  21.                 curl_setopt($ch, CURLOPT_POSTFIELDS, "username=".$username."@126.com&password=".$password);   
  22.                    
  23.                 curl_setopt($ch, CURLOPT_COOKIEJAR, COOKIEJAR);   
  24.                 curl_setopt($ch,CURLOPT_HEADER,1);                   
  25.                 curl_setopt($ch, CURLOPT_TIMEOUT, TIMEOUT);   
  26.                 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);   
  27.                 $str = curl_exec($ch);           
  28.                 //file_put_contents('./126result.txt', $str);                   
  29.                 curl_close($ch);   
  30.                            
  31.                 //获取redirect_url跳转地址,可以从126result.txt中查看,通过正则在$str返回流中匹配该地址   
  32.                 preg_match("/replace\(\"(.*?)\"\)\;/"$str$mtitle);   
  33.                 $_url1 = $mtitle[1];   
  34.                    
  35.                 //file_put_contents('./126resulturl.txt', $redirect_url);           
  36.                 //第二步:再次跳转到到上面$_url1   
  37.                 $ch = curl_init($_url1);                   
  38.                    
  39.                 curl_setopt($ch, CURLOPT_TIMEOUT, TIMEOUT);   
  40.                 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);   
  41.                 curl_setopt($ch,CURLOPT_COOKIEFILE,COOKIEJAR);   
  42.                 curl_setopt($ch, CURLOPT_COOKIEJAR, COOKIEJAR);                   
  43.                 curl_setopt($ch,CURLOPT_HEADER,1);           
  44.                 $str2 = curl_exec($ch);   
  45.                 curl_close($ch);   
  46.                                                    
  47.                 if (strpos($contents"安全退出") !== false)   
  48.                 {                           
  49.                         return 0;   
  50.                 }                   
  51.                 return 1;   
  52.         }   
  53.            
  54.         /**  
  55.          * 获取邮箱通讯录-地址  
  56.          * @param $user  
  57.          * @param $password  
  58.          * @param $result  
  59.          * @return array  
  60.          */  
  61.         public function getAddressList($username$password)   
  62.         {                   
  63.                 if (!$this->login($username$password))   
  64.                 {   
  65.                         return 0;   
  66.                 }   
  67.                
  68.                 $header = $this->_getheader($username);   
  69.                 if (!$header['sid'])   
  70.         {   
  71.             return 0;   
  72.         }   
  73.            
  74.         //测试找出sid(很重要)和host   
  75.         //file_put_contents('./host.txt', $header['host']);   
  76.         //file_put_contents('./sid.txt', $header['sid']);   
  77.            
  78.                 //开始进入模拟抓取   
  79.                 $ch = curl_init();   
  80.                 curl_setopt($ch, CURLOPT_URL, "http://".$header['host']."/a/s?sid=".$header['sid']."&func=global:sequential");   
  81.                 curl_setopt($ch, CURLOPT_COOKIEFILE, COOKIEJAR);   
  82.                 curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/xml"));   
  83.                 $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>";   
  84.                 curl_setopt($ch, CURLOPT_POST, 1);   
  85.                 curl_setopt($ch, CURLOPT_POSTFIELDS, $str);   
  86.                 curl_setopt($ch, CURLOPT_TIMEOUT, TIMEOUT);   
  87.                 ob_start();   
  88.                 curl_exec($ch);   
  89.                 $contents = ob_get_contents();   
  90.   
  91.                 ob_end_clean();   
  92.                 curl_close($ch);   
  93.                    
  94.         //get mail list from the page information username && emailaddress   
  95.         preg_match_all("/<string\s*name=\"EMAIL;PREF\">(.*)<\/string>/Umsi",$contents,$mails);   
  96.         preg_match_all("/<string\s*name=\"FN\">(.*)<\/string>/Umsi",$contents,$names);   
  97.         $users = array();   
  98.         foreach($names[1] as $k=>$user)   
  99.         {   
  100.             //$user = iconv($user,'utf-8','gb2312');   
  101.             $users[$mails[1][$k]] = $user;   
  102.         }   
  103.         if (!$users)   
  104.         {   
  105.             return '您的邮箱中尚未有联系人';   
  106.         }         
  107.            
  108.         return $users;   
  109.         }   
  110.            
  111.         /**  
  112.     * Get Header info  
  113.     */  
  114.     private function _getheader($username)   
  115.     {   
  116.                 $ch = curl_init();   
  117.                 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");   
  118.                 curl_setopt($ch, CURLOPT_COOKIEFILE, COOKIEJAR);  //当前使用的cookie   
  119.                 curl_setopt($ch, CURLOPT_COOKIEJAR, COOKIEJAR);   //服务器返回的新cookie   
  120.                 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);   
  121.                 curl_setopt($ch, CURLOPT_HEADER, true);   
  122.                 curl_setopt($ch, CURLOPT_NOBODY, true);   
  123.                 $content=curl_exec($ch);   
  124.                    
  125.                 preg_match_all('/Location:\s*(.*?)\r\n/i',$content,$regs);   
  126.         $refer = $regs[1][0];   
  127.         preg_match_all('/http\:\/\/(.*?)\//i',$refer,$regs);                   
  128.         $host = $regs[1][0];   
  129.         preg_match_all("/sid=(.*)/i",$refer,$regs);   
  130.         $sid = $regs[1][0];   
  131.                    
  132.                 curl_close($ch);   
  133.                 return array('sid'=>$sid,'refer'=>$refer,'host'=>$host);   
  134.     }   
  135. }   
  136.   
  137. ?>  
我要啦免费统计