Warning: Undefined array key "HTTP_ACCEPT_LANGUAGE" in /www/wwwroot/01xj.net/wp-content/plugins/wp-ue/main.php on line 13
PHP获取163、gmail、126等邮箱联系人地址 – 我的空间 · 我做主

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

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. 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.                 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”);
  17.                 curl_setopt($ch, CURLOPT_POST, 1);
  18.                 curl_setopt($ch, CURLOPT_POSTFIELDS, “username=”.$username.”@126.com&password=”.$password);
  19.                 curl_setopt($ch, CURLOPT_COOKIEJAR, COOKIEJAR);
  20.                 curl_setopt($ch,CURLOPT_HEADER,1);
  21.                 curl_setopt($ch, CURLOPT_TIMEOUT, TIMEOUT);
  22.                 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  23.                 $str = curl_exec($ch);
  24.                 //file_put_contents(‘./126result.txt’, $str);
  25.                 curl_close($ch);
  26.                 //获取redirect_url跳转地址,可以从126result.txt中查看,通过正则在$str返回流中匹配该地址
  27.                 preg_match(“/replace\(\”(.*?)\”\)\;/”, $str, $mtitle);
  28.                 $_url1 = $mtitle[1];
  29.                 //file_put_contents(‘./126resulturl.txt’, $redirect_url);
  30.                 //第二步:再次跳转到到上面$_url1
  31.                 $ch = curl_init($_url1);
  32.                 curl_setopt($ch, CURLOPT_TIMEOUT, TIMEOUT);
  33.                 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  34.                 curl_setopt($ch,CURLOPT_COOKIEFILE,COOKIEJAR);
  35.                 curl_setopt($ch, CURLOPT_COOKIEJAR, COOKIEJAR);
  36.                 curl_setopt($ch,CURLOPT_HEADER,1);
  37.                 $str2 = curl_exec($ch);
  38.                 curl_close($ch);
  39.                 if (strpos($contents, “安全退出”) !== false)
  40.                 {
  41.                         return 0;
  42.                 }
  43.                 return 1;
  44.         }
  45.         /**
  46.          * 获取邮箱通讯录-地址
  47.          * @param $user
  48.          * @param $password
  49.          * @param $result
  50.          * @return array
  51.          */
  52.         public function getAddressList($username, $password)
  53.         {
  54.                 if (!$this->login($username, $password))
  55.                 {
  56.                         return 0;
  57.                 }
  58.                 $header = $this->_getheader($username);
  59.                 if (!$header[‘sid’])
  60.         {
  61.             return 0;
  62.         }
  63.         //测试找出sid(很重要)和host
  64.         //file_put_contents(‘./host.txt’, $header[‘host’]);
  65.         //file_put_contents(‘./sid.txt’, $header[‘sid’]);
  66.                 //开始进入模拟抓取
  67.                 $ch = curl_init();
  68.                 curl_setopt($ch, CURLOPT_URL, “http://”.$header[‘host’].”/a/s?sid=”.$header[‘sid’].”&func=global:sequential”);
  69.                 curl_setopt($ch, CURLOPT_COOKIEFILE, COOKIEJAR);
  70.                 curl_setopt($ch, CURLOPT_HTTPHEADER, array(“Content-Type: application/xml”));
  71.                 $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>”;
  72.                 curl_setopt($ch, CURLOPT_POST, 1);
  73.                 curl_setopt($ch, CURLOPT_POSTFIELDS, $str);
  74.                 curl_setopt($ch, CURLOPT_TIMEOUT, TIMEOUT);
  75.                 ob_start();
  76.                 curl_exec($ch);
  77.                 $contents = ob_get_contents();
  78.                 ob_end_clean();
  79.                 curl_close($ch);
  80.         //get mail list from the page information username && emailaddress
  81.         preg_match_all(“/<string\s*name=\”EMAIL;PREF\”>(.*)<\/string>/Umsi”,$contents,$mails);
  82.         preg_match_all(“/<string\s*name=\”FN\”>(.*)<\/string>/Umsi”,$contents,$names);
  83.         $users = array();
  84.         foreach($names[1] as $k=>$user)
  85.         {
  86.             //$user = iconv($user,’utf-8′,’gb2312′);
  87.             $users[$mails[1][$k]] = $user;
  88.         }
  89.         if (!$users)
  90.         {
  91.             return ‘您的邮箱中尚未有联系人’;
  92.         }
  93.         return $users;
  94.         }
  95.         /**
  96.     * Get Header info
  97.     */
  98.     private function _getheader($username)
  99.     {
  100.                 $ch = curl_init();
  101.                 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”);
  102.                 curl_setopt($ch, CURLOPT_COOKIEFILE, COOKIEJAR);  //当前使用的cookie
  103.                 curl_setopt($ch, CURLOPT_COOKIEJAR, COOKIEJAR);   //服务器返回的新cookie
  104.                 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  105.                 curl_setopt($ch, CURLOPT_HEADER, true);
  106.                 curl_setopt($ch, CURLOPT_NOBODY, true);
  107.                 $content=curl_exec($ch);
  108.                 preg_match_all(‘/Location:\s*(.*?)\r\n/i’,$content,$regs);
  109.         $refer = $regs[1][0];
  110.         preg_match_all(‘/http\:\/\/(.*?)\//i’,$refer,$regs);
  111.         $host = $regs[1][0];
  112.         preg_match_all(“/sid=(.*)/i”,$refer,$regs);
  113.         $sid = $regs[1][0];
  114.                 curl_close($ch);
  115.                 return array(‘sid’=>$sid,’refer’=>$refer,’host’=>$host);
  116.     }
  117. }
  118. ?>

您可能还喜欢...

发表回复