<div class="title_bot"><script>ec(2);</script></div> <ol> <li> <?php </li> </li> <li>/**</li> <li>* @file class.126http.php</li> <li>* 获得126邮箱通讯录列表</li> <li>* @author jvones<jvones> http://www.jvones.com/blog</jvones> </li> <li>* @date 2009-09-26</li> <li>**/</li> <li> </li> <li>class http126</li> <li>{</li> <li> </li> <li> private function login($username, $password)</li> <li> { </li> <li> //第一步:初步登陆</li> <li> $cookies = array();</li> <li> $ch = curl_init();</li> <li> curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);</li> <li> </li> <li> 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");</li> <li> curl_setopt($ch, CURLOPT_POST, 1);</li> <li> curl_setopt($ch, CURLOPT_POSTFIELDS, "username=".$username."@126.com&password=".$password);</li> <li> </li> <li> curl_setopt($ch, CURLOPT_COOKIEJAR, COOKIEJAR);</li> <li> curl_setopt($ch,CURLOPT_HEADER,1); </li> <li> curl_setopt($ch, CURLOPT_TIMEOUT, TIMEOUT);</li> <li> curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);</li> <li> $str = curl_exec($ch); </li> <li> //file_put_contents('./126result.txt', $str); </li> <li> curl_close($ch);</li> <li> </li> <li> //获取redirect_url跳转地址,可以从126result.txt中查看,通过正则在$str返回流中匹配该地址</li> <li> preg_match("/replace("(.*?)");/", $str, $mtitle);</li> <li> $_url1 = $mtitle[1];</li> <li> </li> <li> //file_put_contents('./126resulturl.txt', $redirect_url); </li> <li> //第二步:再次跳转到到上面$_url1</li> <li> $ch = curl_init($_url1); </li> <li> </li> <li> curl_setopt($ch, CURLOPT_TIMEOUT, TIMEOUT);</li> <li> curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);</li> <li> curl_setopt($ch,CURLOPT_COOKIEFILE,COOKIEJAR);</li> <li> curl_setopt($ch, CURLOPT_COOKIEJAR, COOKIEJAR); </li> <li> curl_setopt($ch,CURLOPT_HEADER,1); </li> <li> $str2 = curl_exec($ch);</li> <li> curl_close($ch);</li> <li> </li> <li> if (strpos($contents, "安全退出") !== false)</li> <li> { </li> <li> return 0;</li> <li> } </li> <li> return 1;</li> <li> }</li> <li> </li> <li> /**</li> <li> * 获取邮箱通讯录-地址</li> <li> * @param $user</li> <li> * @param $password</li> <li> * @param $result</li> <li> * @return array</li> <li> */</li> <li> public function getAddressList($username, $password)</li> <li> { </li> <li> if (!$this->login($username, $password))</li> <li> {</li> <li> return 0;</li> <li> }</li> <li> </li> <li> $header = $this->_getheader($username);</li> <li> if (!$header['sid'])</li> <li> {</li> <li> return 0;</li> <li> }</li> <li> </li> <li> //测试找出sid(很重要)和host</li> <li> //file_put_contents('./host.txt', $header['host']);</li> <li> //file_put_contents('./sid.txt', $header['sid']);</li> <li> </li> <li> //开始进入模拟抓取</li> <li> $ch = curl_init();</li> <li> curl_setopt($ch, CURLOPT_URL, "http://".$header['host']."/a/s?sid=".$header['sid']."&func=global:sequential");</li> <li> curl_setopt($ch, CURLOPT_COOKIEFILE, COOKIEJAR);</li> <li> curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/xml"));</li> <li> $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>";</li> <li> curl_setopt($ch, CURLOPT_POST, 1);</li> <li> curl_setopt($ch, CURLOPT_POSTFIELDS, $str);</li> <li> curl_setopt($ch, CURLOPT_TIMEOUT, TIMEOUT);</li> <li> ob_start();</li> <li> curl_exec($ch);</li> <li> $contents = ob_get_contents();</li> <li> </li> <li> ob_end_clean();</li> <li> curl_close($ch);</li> <li> </li> <li> //get mail list from the page information username && emailaddress</li> <li> preg_match_all("/<strings>(.*)/Umsi",$contents,$mails);</strings> </li> <li> preg_match_all("/<strings>(.*)/Umsi",$contents,$names);</strings> </li> <li> $users = array();</li> <li> foreach($names[1] as $k=>$user)</li> <li> {</li> <li> //$user = iconv($user,'utf-8','gb2312');</li> <li> $users[$mails[1][$k]] = $user;</li> <li> }</li> <li> if (!$users)</li> <li> {</li> <li> return '您的邮箱中尚未有联系人';</li> <li> } </li> <li> </li> <li> return $users;</li> <li> }</li> <li> </li> <li> /**</li> <li> * Get Header info</li> <li> */</li> <li> private function _getheader($username)</li> <li> {</li> <li> $ch = curl_init();</li> <li> 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");</li> <li> curl_setopt($ch, CURLOPT_COOKIEFILE, COOKIEJAR); //当前使用的cookie</li> <li> curl_setopt($ch, CURLOPT_COOKIEJAR, COOKIEJAR); //服务器返回的新cookie</li> <li> curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);</li> <li> curl_setopt($ch, CURLOPT_HEADER, true);</li> <li> curl_setopt($ch, CURLOPT_NOBODY, true);</li> <li> $content=curl_exec($ch);</li> <li> </li> <li> preg_match_all('/Location:s*(.*?)rn/i',$content,$regs);</li> <li> $refer = $regs[1][0];</li> <li> preg_match_all('/http://(.*?)//i',$refer,$regs); </li> <li> $host = $regs[1][0];</li> <li> preg_match_all("/sid=(.*)/i",$refer,$regs);</li> <li> $sid = $regs[1][0];</li> <li> </li> <li> curl_close($ch);</li> <li> return array('sid'=>$sid,'refer'=>$refer,'host'=>$host);</li> <li> }</li> <li>}</li> <li> </li> <li>?></li> </ol>